Introduction To Java
Introduction To Java
• Data types
• Variables
• Arrays
• Operators
• Control Statements
Simple Java Program
1 /**
2 * This is the first sample program in Core Java Chapter 3
3 * @version 1.01 1997-03-22
4 * @author Gary Cornell
5 */
6 public class FirstSample
7{
8 public static void main(String[] args)
9{
10 System.out.println("We will not use 'Hello, World!'");
11 }
12 }
Data Types
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
Booleans
// Demonstrate boolean values.
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Integer Literals
• Binary (0b or 0B)
• Octal
• Hexadecimal
• int x = 0b1010;
• int x = 123_456_789;
Floating point Literals
• E or e followed by a decimal number
• 6.022E23, 314159E–05, and 2e+100
• Hexadecimal floating-point literals 0x12.2P2
Character Literals
• They are 16-bit values that can be converted into integers and
manipulated with the integer operators, such as the addition and
subtraction operators.
• All of the visible ASCII characters can be directly entered inside the
quotes, such as 'a', 'z', and '@‘.
• \" for the single-quote character itself and ' \n' for the newline
character.
• ‘\141‘ Octal notation
• \u0061‘ Hexadecimal Notation
String Literals
Variables
• The variable is the basic unit of storage in a Java program.
• A variable is defined by the combination of an identifier, a type, and an optional
initializer.
• In addition, all variables have a scope, which defines their visibility, and a lifetime.
Declaring a Variable
type identifier [ = value ][, identifier [= value ] …];
Example:
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
Dynamic Initialization
Applied to the integer types, long, int, short, char, and byte
Relational Operators
Boolean Logical Operators
Operator Precedence
Control Statements
case valueN :
// statement sequence
break;
default:
// default statement sequence
}
While
do-while
do {
while(condition) {
// body of loop
// body of loop } while (condition);
}
// The target of a loop can be empty. // Demonstrate the do-while loop.
class NoBody { class DoWhile {
public static void main(String args[]) { public static void main(String args[]) {
int i, j; int n = 10;
i = 100; do {
j = 200; System.out.println("tick " + n);
// find midpoint between i and j n--;
while(++i < --j); // no body in this loop } while(n > 0);
System.out.println("Midpoint is " + i); }
} }
}
do {
System.out.println("tick " + n);
} while(--n > 0);
For
// Demonstrate continue.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
}
}
}
return
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}