Unit - 1 Basic Syntactical Constructs in Java.: Mundhe Shankar.G. Lecturer in IT Government Polytechnic Nanded
Unit - 1 Basic Syntactical Constructs in Java.: Mundhe Shankar.G. Lecturer in IT Government Polytechnic Nanded
Unit - 1 Basic Syntactical Constructs in Java.: Mundhe Shankar.G. Lecturer in IT Government Polytechnic Nanded
Mundhe Shankar.G.
Lecturer In IT
Government polytechnic Nanded .
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991.
This language was initially called “Oak,” but was renamed “Java” in 1995.
primary motivation was the need for a platform-independent (that is, architecture-neutral)
language.
Applet:
An applet is a special kind of Java program that is designed to be transmitted over the
Internet and automatically executed by a Java-compatible web browser.
Secure :
Java achieved this protection by confining an applet to the Java execution environment
and not allowing it access to other parts of the computer.
Portability:
Portability is a major aspect of the Internet.
System
System is a predefined class that provides access to the system.
out
out is the output stream that is connected to the console.
println()
Output is actually accomplished by the built-in println( ) method.
class Example2 {
public static void main(String args []) {
int num;
num = 100;
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.print(num);
}
}
Output
This is num: 100
The value of num * 2 is 200
• Literals
• A constant value in Java is created by using a literal
representation of it. For example, here are some literals:
• E.g Integer literal - 100,
float literal 12.43,
char literal ‘c’ ,
String literal “This is a test”
Comments
// Single line comment
/* Multi line
comment
*/
Java Keywords
Java Spacers
Data types
• Java defines eight primitive types of data:
byte, short, int, long, char, float, double, and boolean.
2. Floating-point numbers This group includes float and double, which represent numbers
with fractional precision.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8;
pi = 3.1416;
a = pi * r * r;
System.out.println("Area of circle is " + a);
}
}
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: “ + ch1 + “ " + ch2);
}
}
Output
The output :
ch1 contains X
ch1 is now Y
// 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);
}
}
Output:
B is false
B is true
Variables
The variable is the basic unit of storage in a Java program.
Declaring a Variable
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
Scope 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) {
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
System.out.println("x is " + x);
}
}
// Demonstrate lifetime of a variable.
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
The output generated by this program is shown here:
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
// This program will not compile
class ScopeErr {
public static void main(String args[]) {
int bar = 1;
{
// creates a new scope
int bar = 2; // Compile-time error – bar already defined!
}
}
}
Type Conversion and Casting
• There are two types of conversion 1)Automatic 2) Casting
• If the two types are compatible, then Java will perform the conversion
automatically.
– E.g. assign an int value to a long variable.
• is no automatic conversion defined from double to byte
• Casting -
When these two conditions are met, a widening conversion takes place.
E.g. int type is always large enough to hold byte values, so no explicit cast
statement is required.
– int a;
– byte b;
– b = (byte) a; // Casting done
}
Multidimensional Arrays
• int twoD[][] = new int[4][5];
class Modulus {
public static void main(String args []) {
int x = 42;
int ans= x%10;;
double y = 42.25;
double ans1=y %10;
System.out.println("x mod 10 = " + ans);
System.out.println("y mod 10 = " + ans1);
}
}
output:
x mod 10 = 2
y mod 10 = 2.25
• a = a + 4; or a += 4;
class OpEquals {
public static void main(String args[]) {
int a = 1; int b = 2;
a += 5;
b *= 4;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
• The output of this program is shown here:
• a=6
• b=8
• Bitwise operators
• Relational Operators
• Turnary operator ?
(Expression1 ? Expression 2: Expression 3);
Where Expression1 results true or false
conditional if or Turnary operator
}
Output:
b=10
• instanceof operator
• operator is used for object reference variables only, and you can use it to
check whether an object is of a particular type.
By type, we mean class or interface type—in other words, if the object
referred to by the variable on the leftside of the operator passes the IS-A test
for the class or interface type.
class A { }
class B extends A {
public static void main (String [] args) {
B b = new B();
if (b instanceof A) {
System.out.print("b is an A");
}
}
• If statement
if (condition)
{
statement1;
}
else{
statement2;
}
Nested If Else statement .
if (condition)
{
if (Condition1)
{
statements;
}
if(condition2) { Statements4;}
}
else{
statement2;
}
• Else If ladder
if(condition) {
statement;
}
else if(condition){
statement;
}
else if(condition){
statement;
}
...
else{
statement;
}
• Switch
switch (expression) {
case 1:
// statement sequence
break;
case 2:
// statement sequence
break;
...
case n :
// statement sequence
break;
default:
// default statement sequence
}
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}}
This program generates the following output:
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
• Nested switch
switch(count) {
case 1:
switch(target)
{
case 0:
System.out.println("target is zero");
break;
case 1:
System.out.println("target is one");
break;
}
break;
case 2:
// ..ements
break;
• While Loop
• class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
When you run this program, it will “tick” ten times:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
• Do- While loop Syntax
do {
// body of loop
} while (condition);
While Loop
• While (condition){
//Statements
//increment or decrement
}
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}
• For loop Syntax
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++) Output will be as like
{ .........
System.out.print("."); ........
.......
}
......
System.out.println(); .....
} ....
} ...
..
} .
• Jump Statement -break ,continue, return
• break
It force immediate termination of a loop, bypassing the conditional expression & any
remaining code in the body of the loop.
When a break statement is encountered inside a loop, the loop is terminated and
program control resumes at the next.
Output : 0 1 2 3 5 6
• The last control statement is return.
• The java.lang package also contains the Math class, which is used to perform
basic mathematical operations.
• max()
method takes two numeric arguments and returns the greater of the
two—for example,
x = Math.max(1024, -5000); // output is 1024.
• round()
Output
absolute vale of x=175
absolute vale of y= 184
absolute vale of -0=0
• exp(double a)
public class JavaMathExample1
{
public static void main(String[] args)
{
double x = 28;
double y = 4;
System.out.println("Maximum number of x and y is: " +Math.max(x, y));
System.out.println(“Minuum number of x& y is ”+ Math.min(x,y));
System.out.println("Square root of y is: " + Math.sqrt(y));
System.out.println("Power of x and y is: " + Math.pow(x, y));
System.out.println("exp of a is: " +Math.exp(x));
}
}