object oriented programmingobject oriented programming lecture # 3-4. contents ... 3. logical values...
TRANSCRIPT
![Page 1: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/1.jpg)
Object Oriented Programming
Lecture # 3-4
![Page 2: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/2.jpg)
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
![Page 3: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/3.jpg)
Java “Hello World” Program
![Page 4: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/4.jpg)
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.
![Page 5: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/5.jpg)
Identifiers
Identifiers are used for class names, method names, and variable names.
Java is case-sensitive so VAL and val are two different identifiers.
![Page 6: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/6.jpg)
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:
![Page 7: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/7.jpg)
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
![Page 8: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/8.jpg)
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.
![Page 9: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/9.jpg)
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
![Page 10: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/10.jpg)
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:
![Page 11: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/11.jpg)
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:
![Page 12: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/12.jpg)
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.
![Page 13: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/13.jpg)
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.
![Page 14: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/14.jpg)
Example Program
![Page 15: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/15.jpg)
Floating-Point Data Types
float
double
![Page 16: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/16.jpg)
Example Program
![Page 17: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/17.jpg)
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.
![Page 18: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/18.jpg)
Example Program
![Page 19: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/19.jpg)
Example Program
![Page 20: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/20.jpg)
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
![Page 21: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/21.jpg)
Example Program
![Page 22: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/22.jpg)
Summary of Primitive Data Types
![Page 23: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/23.jpg)
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
![Page 24: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/24.jpg)
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.
![Page 25: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/25.jpg)
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.
![Page 26: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/26.jpg)
Type Casting - Example Program
![Page 27: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/27.jpg)
Automatic Type Promotion
Why Automatic type promotion takes place?
Consider the following example.
![Page 28: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/28.jpg)
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.
![Page 29: Object Oriented ProgrammingObject Oriented Programming Lecture # 3-4. Contents ... 3. Logical values that can be true or false. Integer Data Types](https://reader034.vdocuments.mx/reader034/viewer/2022042803/5f4d5939152ea659d10d4014/html5/thumbnails/29.jpg)
Thank You!