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

Introduction To Java

Uploaded by

maranj006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Introduction To Java

Uploaded by

maranj006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Unit – IV

JAVA PROGRAMMING LANGUAGE

• 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

• Java is a strongly typed language.


• There are eight primitive types in Java.
• Four of them are integer types;
• Two are floating-point number types;
• one is the character type char, used for code units in the Unicode
encoding scheme and
• one is a Boolean.
Integer types
Floating-Point Types
Characters

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

// Demonstrate dynamic initialization.


class DynInit {
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);
}
}
The Scope and Lifetime of Variables
// Demonstrate block scope.
class Scope
{
public static void main(String args[])
{
int x; // known to all code within main
x = 10;
if(x == 10)
{ // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
Type Conversion and Casting
Implicit or Automatic Conversions or System.out.println("\nConversion of int to byte.");
b = (byte) i;
widening conversion System.out.println("i and b " + i + " " + b);
• Ex: int value to a long System.out.println("\nConversion of double to int.");
i = (int) d;
Explicit or narrowing conversion  System.out.println("d and i " + d + " " + i);
from double to byte System.out.println("\nConversion of double to byte.");
b = (byte) d;
(target-type) value System.out.println("d and b " + d + " " + b);
}
}
Example:
• class Conversion {
• public static void main(String args[]) {
• byte b;
• int i = 257;
• double d = 323.142;
Arrays

• An array is a group of like-typed variables that are referred to by a


common name.
• Arrays of any type can be created and may have one or more
dimensions.
• A specific element in an array is accessed by its index. Arrays offer a
convenient means of grouping related information.
type var-name[ ]; or type[ ] var-name;
array-var = new type [size];
// Manually allocate differing size for(i=0; i<4; i++)
second dimensions. for(j=0; j<i+1; j++) {
class TwoDAgain { twoD[i][j] = k;
public static void main(String args[]) { k++;
}
int twoD[][] = new int[4][];
for(i=0; i<4; i++) {
twoD[0] = new int[1]; for(j=0; j<i+1; j++)
twoD[1] = new int[2]; System.out.print(twoD[i][j] + " ");
twoD[2] = new int[3]; System.out.println();
}
twoD[3] = new int[4];
}
int i, j, k = 0; }
Operators
Arithmetic Operators
The Bitwise Operators

Applied to the integer types, long, int, short, char, and byte
Relational Operators
Boolean Logical Operators
Operator Precedence
Control Statements

• Selection Statements(if and switch)


• Iteration Statements(for, while, and do-while)
• Jump Statements (break, continue, and return)
If if (condition) statement1;
else statement2;

The if-else-if Ladder


if(condition)
Nested ifs statement;
if(i == 10) { else if(condition)
if(j < 20) a = b; statement;
if(k > 100) c = d; // this if is else if(condition)
else a = c; // associated with this else statement;
} .
else a = d; // this else refers to if(i == 10) .
.
else
statement;
switch (expression) {
Switch case value1:
// statement sequence
break;
• multiway branch statement. case value2:
// statement sequence
• better alternative than a large break;
series of if-else-if 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

// Test for primes.


for(initialization; condition; iteration) { class FindPrime {
// body public static void main(String args[]) {
int num;
} boolean isPrime;
num = 14;
if(num < 2) isPrime = false;
Using the Comma else isPrime = true;
class Comma { for(int i=2; i <= num/i; i++) {
public static void main(String args[]) { if((num % i) == 0) {
int a, b; isPrime = false;
break;
for(a=1, b=4; a<b; a++, b--) {
}
System.out.println("a = " + a);
}
System.out.println("b = " + b); if(isPrime) System.out.println("Prime");
} else System.out.println("Not Prime");
} }
} }
The For-Each Version of the for Loop
for(type itr-var : collection) statement-block

// Use a for-each style for loop.


class ForEach {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// use for-each style for to display and sum the values
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
}
System.out.println("Summation: " + sum);
}
Nested Loops

// Loops may be nested.


class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();
}
}
}
Using break
// Using break with nested loops. Using break as a Form of Goto
class BreakLoop3 {
// Using break as a civilized form of goto.
public static void main(String args[]) { class Break {
public static void main(String args[]) {
for(int i=0; i<3; i++) {
boolean t = true;
System.out.print("Pass " + i + ": "); first: {
for(int j=0; j<100; j++) { second: {
third: {
if(j == 10) break; // terminate loop if j is 10 System.out.println("Before the break.");
System.out.print(j + " "); if(t) break second; // break out of second block
System.out.println("This won't execute");
} }
System.out.println(); System.out.println("This won't execute");
}
} System.out.println("This is after second block.");
System.out.println("Loops complete."); }
}
} }
}
Using continue

// 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.");
}
}

You might also like