This document summarizes key points from a lecture on Java programming concepts including constants, data types, variables, and more. It discusses Java's primitive data types including numeric, character, boolean types and literals. It also covers variable declaration and scope, and provides examples of using variables and data types in simple Java programs. The document is presented as lecture notes with definitions and code examples across multiple slides.
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 ratings0% found this document useful (0 votes)
69 views19 pages
Lecture 3
This document summarizes key points from a lecture on Java programming concepts including constants, data types, variables, and more. It discusses Java's primitive data types including numeric, character, boolean types and literals. It also covers variable declaration and scope, and provides examples of using variables and data types in simple Java programs. The document is presented as lecture notes with definitions and code examples across multiple slides.
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/ 19
Lecture Three
Second Java Program,
Constants, Data Types and Variables 12/18/20 ASHISH BAJPAI ,Assistant 1 professor Department of CSE, First Java Program-Example 1 /*This is a simple java program*/ class Example { public static void main (String args[]) { System.out.println (“This is a simple Java program”); } }
ASHISH BAJPAI 2 12/18/20
,Assistant professor Simple Java Program-Some important points public: Access specifier. main() must be made public, since it must be called by code defined outside it’s class. Static: It is required because main() is called without creating an object of it’s class String args[]: An array of objects of type String class. Each object of type string contains a character string. It is used to manipulate command line argument. Java is case sensitive. System predefined class that refers to system. out It is static data member of System class println() It is a member 3of out object ASHISH BAJPAI 12/18/20 ,Assistant professor Second Java Program class add_num public class test { { public int add(int a, int b) public static void main(String args[]) { { return a+b; int a, b; } a=100; } b=23; add_num ob = new add_num(); System.out.println(“The addition: “+ ob.add_num(a,b));
} }
12/18/20 ASHISH BAJPAI ,Assistant 4
professor Department of CSE, Points to be Noted Java source code can contain multiple classes, the name of the source code must be the name of the only public class of the code. Usually public class contains the main() method, that is the starting point of program execution. 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.
12/18/20 ASHISH BAJPAI ,Assistant 5
professor Department of CSE, Java is a Strongly typed Language 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.
12/18/20 ASHISH BAJPAI ,Assistant 6
professor Department of CSE, The Primitive Types 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
12/18/20 ASHISH BAJPAI ,Assistant 7
professor Department of CSE, Numeric Primitive Types The difference between the various numeric primitive types is their size, and therefore the values they can store:
Type Storage Min Value Max Value
byte 8 bits -128 127
short 16 bits -32,768 32,767 int 32 bits -2,147,483,648 2,147,483,647 long 64 bits < -9 x 1018 > 9 x 1018
float 32 bits +/- 3.4 x 1038 with 7 significant digits
double 64 bits +/- 1.7 x 10308 with 15 significant digits 12/18/20 ASHISH BAJPAI ,Assistant 8 professor Department of CSE, Character Primitive Type 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 12/18/20 ASHISH BAJPAI ,Assistant 9 professor Department of CSE, Character Primitive Type 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 12/18/20 ASHISH BAJPAI ,Assistant 10 professor Department of CSE, Booleans 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
12/18/20 ASHISH BAJPAI ,Assistant 11
professor Department of CSE, Literals 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.
12/18/20 ASHISH BAJPAI ,Assistant 12
professor Department of CSE, Literals 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.
12/18/20 ASHISH BAJPAI ,Assistant 13
professor Department of CSE, Literals Character Literals: • A literal character is represented inside a pair of single quotes.
Escape sequence Unicode Description
Representation 1. \’ \u0027 Single quote 2. \” \u0022 Double quote 3. \\ \u005c Backslash 4. \r \u000d Carriage Return 5. \n \u000a New line 6. \f \u000b Form feed 7. \t \u0009 Tab 8. \b \u0008 Backspace 9. \ddd Octal Character 10. \uxxxx Hexadecimal Unicode character
12/18/20 ASHISH BAJPAI ,Assistant 14
professor Department of CSE, Literals String Literals • A sequence of characters between a pair of double quotes. • In java string must begin and end on the same line.
12/18/20 ASHISH BAJPAI ,Assistant 15
professor Department of CSE, Variables 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. 12/18/20 ASHISH BAJPAI ,Assistant 16 professor Department of CSE, Scope and Lifetime of a variable 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 it’s value once it has gone out of it’s scope. In an inner block, it is not possible to declare a variable of the same name as in outer block. 12/18/20 ASHISH BAJPAI ,Assistant 17 professor Department of CSE, Scope and Lifetime of a variable 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); }
12/18/20 ASHISH BAJPAI ,Assistant professor Department of CSE, KIET 18
Chapter Three -Book of Herbert Schieldt Read about keyword, identifier, character set yourself.