object oriented programmingobject oriented programming lecture # 3-4. contents ... 3. logical values...

Post on 16-Jul-2020

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Object Oriented Programming

Lecture # 3-4

Contents

Java “Hello World” Program

Identifiers: Valid and invalid identifiers

Variables and Data types

The eight primitive types

Integer Data Types

Floating Point Data Types

Character

Booleans

Type Conversion and Casting

Java “Hello World” Program

Decoding: Java “Hello World” Program

• class – keyword for making a javaClass

•HelloWorld – name of the class

•public, static, String and void -keywords

•main ()- main( )is the method calledwhen a Java application begins. Theentry point of the program

•String args[] - receives any command-line arguments present when theprogram is executed.

•Println() – built -in function for printingon the console.

•System is a predefined class thatprovides access to the system, and outis the output stream that is connected to the console.

Identifiers

Identifiers are used for class names, method names, and variable names.

Java is case-sensitive so VAL and val are two different identifiers.

Variables and Data Types

A variable is a program component used to store or represent data.

The number, letter, or other data item in a variable is called its value.

The value for a variable can be changed in the program.

Each variable that you declare can store values only of a type consistent with the data type of that variable.

Example:

The Eight Primitive Types

The only things in Java that are not objects are variables.

The fundamental types are referred to as primitive types.

The values can fall in one of the following category:

1. Numeric values, which can be either integer or floating-point

2. single Unicode character

3. Logical values that can be true or false

Integer Data Types

There are four types of variables that you can use to store integer data.

The value can be –ve or +ve.erson behaves like an EMPLOYEE in office.

Integer Data Types The diagram below shows the number of bits available to store each

type that determines the maximum and minimum values.

Negative binary numbers are represented in what is called 2's complement form

Integer Data Types

Byte

Variables are declared by use of the byte keyword.

For example, the following declares two byte variables called b and c:

Integer Data Types

Short

Short is a signed 16-bit type. It has a range from – 32,768 to 32,767.

It is probably the least-used Java type.

Here are some examples of short variable declarations:

Integer Data Types

Int

The most commonly used integer type is int.

When byte and short values are used in an expression they are promoted to int when the expression is evaluated.

Integer Data Types

Long

long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.

It is useful when big, whole numbers are needed.

Example Program

Floating-Point Data Types

float

double

Example Program

Character

The data type used to store characters is char.

In Java char is a 16-bit type.

The range of a char is 0 to 65,536.

There are no negative chars.

Example Program

Example Program

Booleans

Java has a primitive type, called boolean, for logical values.

It can have only one of two possible values, true or false.

Used with relational operators e.g. a < b.

The true literal in Java does not equal 1, nor does the false literal equal 0

Example Program

Summary of Primitive Data Types

Type Casting and Conversion

Assigning a value of one type to a variable of another type is known as Type Casting.

Example int x = 10; byte y = (byte) x;

In Java, type casting is classified into two types:

Widening Conversion or Implicit Type Casting Narrowing Conversion or Explicit Type Casting

Widening Conversion

When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met:

The two types are compatible.

The destination type is larger than the source type.

Widening Conversion: it takes place when two types are compatible

e.g. byte value assigned to int type.

There are no automatic conversions from the numeric types to char or boolean.

Narrowing Conversion

A cast is simply an explicit type conversion.

Narrowing conversion takes place when a value of larger data type is assigned to the variable of smaller data type.

E.g. An int value to a byte variable.

Type Casting - Example Program

Automatic Type Promotion

Why Automatic type promotion takes place?

Consider the following example.

Automatic Type Promotion Rules

All byte, short, and char values are promoted to int.

Then, if one operand is a long, the whole expression is promoted to long.

If one operand is a float, the entire expression is promoted to float.

If any of the operands is double, the result is double.

Thank You!

top related