0% found this document useful (0 votes)
91 views17 pages

Lecture Two: Second Java Program, Constants, Data Types and Variables

This document summarizes key points from a lecture on Java programming. It discusses constants, data types, variables, and scope in Java. The lecture introduces a simple Java program that adds two numbers, and covers primitive data types like int, float, boolean and char. It also discusses variable declaration and scope, literals, and the difference between Java as a strongly typed vs weakly typed language.

Uploaded by

Fahim Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views17 pages

Lecture Two: Second Java Program, Constants, Data Types and Variables

This document summarizes key points from a lecture on Java programming. It discusses constants, data types, variables, and scope in Java. The lecture introduces a simple Java program that adds two numbers, and covers primitive data types like int, float, boolean and char. It also discusses variable declaration and scope, literals, and the difference between Java as a strongly typed vs weakly typed language.

Uploaded by

Fahim Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture Two

SECOND JAVA PROGRAM, CONSTANTS, DATA TYPES AND VARIABLES


Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Second Java Program


2

class Add { int add_num(int a, int b) { return a+b; } }

class Test { public static void main(String args[]) { int a, b; a=100; b=23; Add ob = new Add(); System.out.println(The addition: + ob.add_num (a,b)); } }

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Points to be Noted
3

Java source code can contain multiple classes, the name

of the source code must be the name of the class containing the main method.
Object of a class is created using new operator. Method of a class is accessed through an object of that

class.
+ in System.out.println() method concatenate two

strings.
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Java is a Strongly typed Language


4

Every variable and expression has a strongly defined

type.
All assignments are checked for type compatibility.
Java compiler checks all expressions and parameters

to ensure that the types are compatible.

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

The Primitive Types


5

There are exactly eight primitive data types in Java Four of them represent whole valued signed numbers:

byte, short, int, long

Two of them represent floating point numbers:

float, double

One of them represents characters:

char

And one of them represents boolean values:

boolean
25/3/2010

Sarker Tanveer Ahmed, CSE, DU

Numeric Primitive Types


6

The difference between the various numeric primitive

types is their size, and therefore the values they can store:
Type
byte short int long float double

Storage
8 bits 16 bits 32 bits 64 bits 32 bits 64 bits

Min Value
-128 -32,768 -2,147,483,648 < -9 x 1018

Max Value
127 32,767 2,147,483,647 > 9 x 1018

+/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits
25/3/2010

Sarker Tanveer Ahmed, CSE, DU

Character Primitive Type


7

It uses unicode to represent character. The char type is unsigned 16 bit values ranging from 0 to 65536. ASCII still ranges from 0 to 127.

Example: class test { public static void main (String args[]) { char ch1, ch2; ch1=88; ch2=Y; System.out.println (ch1 and ch2: + ch1+ +ch2); } } Output: ch1 and ch2: X Y
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Character Primitive Type


8

Example: class test { public static void main (String args[]) { char ch1; ch1= X; Sytem.out.println (ch contains +ch1); ch1++; System.out.println (ch1 is now + ch1); } } Output: ch1 contains X Ch1 is now Y

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Booleans
9

Size is 1 bit two value: true and false. This type is returned by all relational operators. Example: boolean b; b= true; 1. System.out.println(b is +b); 2. System.out.println(10>9 is +(10>9)); Output: b is true 10>9 is true

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Literals
10

Integer Literals

1. base 10 1,2,43 etc. 2. base 8 octal values are denoted in java by a leading 0. 3. base 16 hexadecimal values are denoted by leading 0x or 0X. Any whole number is by default integer (32 bits). To specify a long literal, the number should appended with an upper- or lowercase L.

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Literals
11

Floating point Literals

1. Standard Notation 3.14159, 0.6667, 2.0 etc. 2. Scientific Notation 6.022E23, 2e+100. Floating point literals are by default of type double. To specify a float literal, we must append an F or f to the constant. Boolean Literals Two values true and false. True is not equal 1 and false is not equal to 0. They can be assigned to variable declared as boolean.
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Literals
Character Literals: A literal character is represented inside a pair of single quotes.
Escape sequence
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. \ \ \\ \r \n \f \t \b \ddd \uxxxx

Unicode Representation
\u0027 \u0022 \u005c \u000d \u000a \u000b \u0009 \u0008

Description
Single quote Double quote Backslash Carriage Return New line Form feed Tab Backspace Octal Character Hexadecimal Unicode character

25/3/2010

Sarker Tanveer Ahmed, CSE, DU

12

Literals
13

String Literals A sequence of characters between a pair of double

quotes. In java string must begin and end on the same line.

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Variables
14

Variable is a name for a location in memory. Declaring a variable:

type identifier [=value][,identifier [=value].];


The initialization expression must result in a value of the

same or compatible type as that specified for the variable.


When a variable is not initialized, the value of that

variable is undefined.
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Scope and Lifetime of a variable


15

A block begins with an opening curly brace and ends by a closing

curly brace. A block determines scope, that defines which objects are visible to other parts of your program. Variables declared within a block localize themselves. In case of nested block, the outer block encloses the inner block. The variables declared in the outer block is visible to the inner block but the reverse is not true. A variable will not hold its value once it has gone out of its scope. In an inner block, it is not possible to declare a variable of the same name as in outer block.
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

Scope and Lifetime of a variable


16

Example: public static void main( String args[]) { int x =10; if ( x == 10) { int y =20; System.out.println(x and y: +x + +y); x= y * 2; } y= 100; //Error System.out.println (x is +x); }
Sarker Tanveer Ahmed, CSE, DU

25/3/2010

17

Chapter Three

-Book of Herbert Schieldt Read about keyword, identifier, character set yourself.

Sarker Tanveer Ahmed, CSE, DU

25/3/2010

You might also like