0% found this document useful (0 votes)
54 views

3 Data Types and Variables

This document discusses Java data types including primitive types like integers, floating point numbers, characters, and booleans. It covers integer types in more detail including their sizes and how expressions involving different integer types are promoted to integers before calculation. It also discusses floating point types, characters and strings, and boolean types. Finally, it discusses variables, their scope and lifetime, and type conversion and casting between incompatible types.

Uploaded by

Ghous Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

3 Data Types and Variables

This document discusses Java data types including primitive types like integers, floating point numbers, characters, and booleans. It covers integer types in more detail including their sizes and how expressions involving different integer types are promoted to integers before calculation. It also discusses floating point types, characters and strings, and boolean types. Finally, it discusses variables, their scope and lifetime, and type conversion and casting between incompatible types.

Uploaded by

Ghous Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Advanced Programming

Engr Faiza Tila


Data Types

Java
Primitive Types
• Form the basis of all other types of data that you can create
– Integer types
• For whole-valued signed numbers
• byte, short, int and long
– Floating-point types
• Numbers with fractional precision
• float and double
– Characters
• Represents symbols in a character set
• char
– Boolean
• A special type representing true / false
• boolean
Integers
• Does not support unsigned, positive-only
integers
– long – 64 bit
– int – 32 bit (most commonly used)
– short – 16 bit (rarely used)
– byte – 8 bit (smallest, -128 to 127)

• If an expression involves bytes, shorts, ints and


literal numbers  the entire expression is
promoted to int before calculation is done
Floating-Points
• Real numbers
– double – 64 bits
• When u need to maintain accuracy over many
iterative calculations
– float – 32 bits
• When require fractional component but not very
high degree of precision
Characters
• char is not the same as C/C++
– In C/C++, char is an integer type, 8-bits wide
• Only represents ASCII
– In Java, char is 16-bits wide
• Unicode
• Range : 0 – 65536 (unsigned!)
• to represent all characters in different human languages

• We can perform increment and addition as integers


char ch1 = ‘X’;
System.out.println(“ch1 contains” + ch1); // prints X
ch1++;
System.out.println(“ch1 is now ” + ch1); // prints Y
Booleans
• Represent logical values
– true or false
– All relational operators return boolean value
• <,>,==,
– All conditional expressions require boolean
value
• if , for , while
String Literals
• Specified like in most other languages
– Enclosing a sequence of characters between pair of
double quotes
• “Hello World”
• “two\nlines”
• “ \“ This is in quotes \” ”
– Escape character  \
• \n , \r , \\
• Important Difference
– In C/C++, Strings are implemented as arrays of
characters
– However, in Java, Strings are actually object types
Variables

Java
Variables
• Basic unit of storage in Java program
– type identifier [= value][, identifier [= value] …] ;
• type: built-in type, class or interface
• identifier: name of variable
• value: to initialize the variable; must be of same or compatible
type

– Examples
• int a, b, c;
• int d=3, e, f=5;
• byte z=22;
• double pi = 3.14159
• char x = ‘X’;
Dynamic Initialization
• Variables can be initialized dynamically, using any valid
expression

Class DynInt {
public static void main (String [] args) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt (a*a + b*b);
System.out.println(“Hypotenuse is” + c);
}
}
Scope and Lifetime of Variables
• Variable can be declared inside any block
{ }
– A block defines a scope : New block  New scope
• Scopes can be nested
– Variables of outer scope are visible to inner scope but not vice
versa !
• If a variable is visible in a scope, we can not declare
another variable with same identifier in that scope

• Important scopes in java


– Defined in class
– Defined in methods
• Variables declared in a method + Method parameters (input
arguments)
Scope Example
class Scope {
public static void main (String [] args) {
int x = 10; // known to all code in main
if (x == 10) { // start new scope
int y = 20; // known only to this block

// x & y are both known here


x = y * 2;
} // end of scope
// y = 100; // Error ! y not known here

System.out.println(“x is “ + x); // x is still known


}
}
Do we have any other variable inside main(..) method’s scope?
Type Conversion & Casting
• We can assign value of one type to a variable of another
type
– For compatible types  Java performs conversion
automatically
– For incompatible types  cast explicitly

• Two conditions must be met for automatic conversion in


Java
– The two types are compatible
– Destination type is larger than source type
• From int to long = Automatic
• From byte to int = Automatic
– However
• From long to int = NOT Automatic // int is smaller
• From int to byte = NOT Automatic // byte is smaller
Casting Incompatible Types
• A cast is an explicit type conversion
– (target-type) value
– Example
int a;
byte b;
// …
b = (byte) a;

– If interger’s value is larger than range of a byte,


it will be reduced modulo byte’s range
• Remainder of an integer division by the byte’s range
Casting …
• What will happen if we cast a floating point to int ?
– Integers do not support fractions  fractional component is
lost
»Truncation !

• Examples
byte b;
int i = 257;
double d = 323.142;

b = (byte) i; // b contains 257 modulo 256 = 1


i = (int) d; // i contains 323 ; fraction truncated
b = (byte) d; // b contains 323 modulo 256 = 67
// fraction truncated as well
Automatic Type Promotion
• Java promotes some smaller types to larger types
automatically when evaluating expressions
– bytes or short are promoted to int

• Example
byte a=40, b = 50;
int c = a * b;
• a and b promoted to int
• However, following code causes problems
byte b = 50;
b = b * 2; // Error! Cannot assign int to byte!
• since b was promoted to int, b*2 is an ‘int’ now and needs
explicit casting to smaller type ‘byte’
b = (byte) b * 2; // Correct
Rules: Automatic Type Promotion
• Following promotions take place in
expressions

– short and byte  always promoted to int

– Anyone operand is long  all promoted to


long

– Anyone operand is float / double  all to float


/ double

You might also like