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

Java Notes

Java originated from C and C++ but is platform independent. It supports object oriented programming, exception handling, and multi-threading. Java programs are compiled into bytecode that can run on any Java Virtual Machine (JVM), making Java portable across various operating systems and hardware.

Uploaded by

MAHENDRA ZALA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Java Notes

Java originated from C and C++ but is platform independent. It supports object oriented programming, exception handling, and multi-threading. Java programs are compiled into bytecode that can run on any Java Virtual Machine (JVM), making Java portable across various operating systems and hardware.

Uploaded by

MAHENDRA ZALA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

GTU - MCA – SEM 1 JAVA - NOTES

Your code coach


Question :1 Java’s Lineage: C and C++…
C C++ java
-language that - language that -language that gets
needs to be needs to be interpreted and
compile… compile… compile
-platform -platform -platform
dependent dependent independent
-does not support -does not support -it support multi-
multi-thread multi-thread thread
programming programming
-Does not have any -Has exception -Has exception
exception handling handling metalisum handling metalisum
using try-catch using try catch and
throw.
-does not support -support object -support object
object oriented oriented oriented
programming programming programming
-pointers can be -pointers can be -no objection in
used used pointer
-no class -A program can be - There must be at
written without a least one class
class

Your code coach


Question :2 How java impacted the internet.
-java innovated in new type of network program called the
applet that changed the online world
- An applet is a special kind of java program that is designed
to be transmitted over the internet and automatically excuted
by a java compatable web browser
- the creation of the applet changed internet programming
because it expanded the universe of objects that can move
freely in cyber space
-The java achieved the security by confining an applet to the
java execution environment and not allowing access to other
parts of the computers
- portability is a major aspect of the internet because there
are many different type of computer at operating system
connected to it .
-for example, in the case of applet,the same applet must be
able to be downloaded and executed by the wide varities of
the CPU operating System and browsers connected to the
internet
-The same code must work on all computers .

Question :3 Java’s magic : The ByteCode …


-bytecode is a set of instruction designed to be executed by
the java runtime System , which is called java virtual machine
(jvm)
- translating a java program into bytecode makes it easier to
run a program in a wide variety of an environments because
only the jvm needs to be implemented for each platform.
-Although the details of jvm will different from platform to
platform all understand , the same java bytecode.
-Execution of java program by the jvm helps it to make it
secure.
-Use of bytecode enable the jvm to execute the programs
much faster.
-java bytecode is present in .class file.
-Bytecode lies in between low level and highlevel set of
instruction.

Question : 4 The Java Buzzwords & features…


- A list of key features is known as Java Buzz Words.
1. Simple
2. Object-Oriented
3. Distributed
4. Robust
5. Secure
6. Architecture-neutral
7. Portable
8. Interpreted
9. High-performance
10.Multithreaded
11.Dynamic
12.platform independent…

[ 1 ] Simple :

-The java is the simple programming language.

-Java syntax is based on C++ (so easier for programmers


to learn it after C++).

[ 2 ] Portable :

-The Same bytecode Program can run on any computer


System that has java interpreter(i.e.jvm)

[ 3 ] Object Oriented :
-java support various object oriented features such as
data encapsulation , inheritance , polymorphism and
dynamic.

[ 4 ] Robust :

-Java uses Strong memory management .

[ 5 ] Multi-threaded :

-Multi-threaded allows the programmer to write such a


program that can perform many task simultaneously.

[ 6 ] Interpreted :

-java compiler translate the java source program into a


special code called bytecode which is interpreted to
obtain the equivalent machine code.

[ 7 ] platform independent…

-Java is platform independent because it is different from


other languages like C, C++, etc.

- which are compiled into platform specific machines


while Java is a write once, run anywhere language.

- A platform is the hardware or software environment in


which a program runs.
Your code coach

Like , share , and subscribe


Your code coach

Question : 5 Object Oriented programming …


(Encapsulation,Polymorphism,Inheritance)
-Object means a real-world entity such as a pen, chair, table,
computer, watch, etc.

- Object-Oriented Programming is a methodology or


paradigm to design a program using classes and objects.

- It simplifies software development and maintenance by


providing some concepts:

[1] Encapsulation :
- encapsulation is the mechanism that binds together code and
the data , it manipulate and keep both safe from outside
interreference and misuse.
-In java the basic of encapsulation is the class which define the
structure and behaviour (data and code)that will be shared by a
set of object.

[ 2 ] polymorphism :
-polymorphism is the feature that allows us to perform single
action in different ways.
EXAMPLE :
class Animal

public void animalSound()

System.out.println("The animal makes a sound");

class Pig extends Animal

public void animalSound()

System.out.println("The pig says: wee wee");

[ 3 ] Inheritance :
-Inheritance is the mechanism in java by which derived class
can borrow the properties of base class and at the game time
derived class may have some additional properties.
EXAMPLE:
class Vehicle

protected String brand = "Ford";


public void honk()

System.out.println("Tuut, tuut!");

class Car extends Vehicle

private String modelName = "Mustang";

public static void main(String[] args)

Car myFastCar = new Car();

myFastCar.honk();

System.out.println(myFastCar.brand + " " + myFastCar.modelName);

OUTPUT :

Tuut, tuut!
Ford Mustang

Question : 6 obtaining the Java Development kit…


-JDK is a collection of tools that are used for development and
runtime program.
-JDK Consists of :
 Javac : the java compiler which translates the source code
to the byte code from and stores it in a separate class file .

 Java : the java interpreter which interpreters the bytecode


stored in the class and executes the program to generate
the output .

 Javadoc : Javadoc used for creating the html document for


documentation from source code file .

 Javah : it produces the header files for the use of native


method .

 Javadb: java debugger is used to find the error in the


program .

 Applet viewer : it executes java applet .

 Java p : java disassembler which help in converting byte


code files in program description .

Question : 7 A First Simple Program…


Public class Example

Public static void main (String args[])

System.out.println(“this is a simple program”);

Output:

this is a simple program

Question : 8 Handling Syntax Errors…


-When Any kind of the serious problem occurs which could not
be handled easily like out of memory error that is called an
error.
-Syntax error or compile time error:
Errors that are detected by the java compiler during the compile
time are called syntax error or compile time errors when the
compiler issues the error it will not generate the .class file .
-most commonly occurring syntax or compile error are
semicolon (;) , wrong spelling of keyword and identifiers ,
missing brackets ({}), missing double quotes(“ ”)in the String
, use of undeclared variables , use of equal to (=) instead of
equal to equal to (==), incompatible types in assignment
Statement , illegal reference to the object .
EXAMPLE:
Public class value
{
Public static void main (String args[])
{
int a=10;
System.out.println(“the value of a is :”+a)
}
}
Output:
Value.java :error (;)expected System.out.println(“the value of
a is :”+a)1 error.
-syntax or compile time errors are the most common type error
that are encountered.
-this errors occurs when the code violets the rules of the
programming language .

Question : 9 A second simple program :


Class example2
{
public static void main(String args [] )
{
int num;
num=100;
System.out.println(“this is num:”+num);
num=num*2;
System.out.println(“the value of num*2 is :”num);
}
}

OUTPUT :
this is num :100
the value of num*2 is:200

Question : 10 Two control statements :


if statement :
-In simple if statement only one statement is followed by the if
statement.
Syntax :
if (apply some condition statement)
{
}

Example :
public class Main
{
public static void main(String[] args)
{
if (20 > 18)
{
System.out.println("20 is greater than 18"); // obviously
}
}
}

OUTPUT:
20 is greater than 18
-in compound if statement more than one statement can be
executed when if condition is true.
-All this executable statement are placed in curly braces({})
EXAMPLE:
If(age>18)
{statement1,statement2,…statementn}

FOR LOOP :
-“For” is a keyword for repeatedly execute a sequence of code
by creating a loop .
-loops are used whenever you need to perform a repetitive task
Syntax :
for(initialization,condition,iteration)
EXAMPLE :
public class Main
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
}
}
OUTPUT:

0
1
2
3
4

Question : 11 Block Of Code…


-Java allows two or more statements to be grouped into blocks
of code called code block .
-This is done enclosing the statement between opening &
closing curly braces.
EXAMPLE:
If(x<y)
{
//begin a block
x=y;
y=0;
}//end of block
- If (x<y) is true both the statements will be executed
otherwise none of the statement will be executed.
Semicolon : semicolon is a operator that is used to
terminate the statement .
- each individual statement must be ended with a
semicolon (;)
-A block ({}) that is not terminated by semicolon .
Indentation : used to make the code reusable to other users
because it makes the code easier to edit , display , how the
braces matchup , and shows the logic of the program in a
well organized fashion .
-line lengths should be kept less than (<) 80 characters.
Breaking lines : (a)after a coma and before an operator (b)
new line must be a lived at the beginning of the previous
line expression .
EXAMPLE:
Public class

Question : 12 The Java Keywords…


-In Java , keywords are reserved words .
-Keywords cannot be used as names for a variable , class or
method .
-const&goto – this two keywords are not used .
-The following are the keywords:

Question : 13 Identifiers…
-it is used for class name , methods name and variable names .
- Rules to be followed for identifiers :
- Identifiers should be written using alphabet , digits ,
underscore (_) and dollar sign ($)
- Should not contain any other special character with in them .
- Must not start with a digit .
- the identifiers are case sensitive i.e int a ; or int A; are treated
two different entity.
- can be of any length.
Question : 14 Java class libraries…
-Java has two built-in Methods println() and print().
-These methods are accessed through System class , which is a
class predefined by java that is automatically included in
programs i.e System.out.println()
-class libraries much of the functionality that comes in java .

Question : 15 Introducing data types and operators…


#why data types are important:
-As java is strongly typed , language that is all operations are
type – checked by the compiler for type -compatibility to
enable the strong type checking , all variables expressions and
values has datatype.
#java’s primitive data types :

-A primitive data type specifies the size and type


of variable values, and it has no additional
methods.
Integer Size Range
(in bytes)
Int 4 -1247483648 to 2147483648
Floating point 4 1.754qe-038 to 3.40282eto38
characters 2 -32768 to 32767

#Boolean Type :

Size (in bytes) : 1 bit

Range : true or false

#Literals :
-Constant values that appear directly in the program .

Example: int cost =340;

#Hexa-Decimal :
-Sequence of digit precided by ox or ox is considered as hexa-
decimal point , it may also include , a character from ‘a’ to ‘f’ or
‘A’ to ‘F’ that represent 10 respectively that is oxd ,oxf .

#octal :
-it is a combination of a number that has a digit from 0 to 7 with
a leading Zero .

Example : 045,026 etc


#binary :

Base 2 whose digits consist of number 0 and 1

#Character escape sequence :


-character escape sequence is expressed as a character
or an enscape sequence enclosed in a single quote
mark (‘ ’) .example: ‘a’ , ’b’ , ‘%’ , etc.
#String Literals:
-A sequence of characters that is enclosed between (“”)
Double quote mark .
EXAMPLE: “sonali”,”123”,”\n” etc.
#variables:
-A named location in memory that is used to hold a
value that can be modified in the program by the
instruction variable name should not began with digits .
#Initializing a variable :
-It means specifying and initial value to assign to it.
-A variable that is not initialized and not to be used
until it is assigned such a value .
#Dynamic Initialization :
-java allows variable to be Initialized dynamically.
EXAMPLE :
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);
}
}

-computation of length of hypotenuse of a triangle


given the lengths of its two opposite side is calculated .
- c is initialize dynamically .

#scope and lifetime of variable :


-A block defines a scope each time a new block is
started a new scope is created .
- A scope determines what objects are visible to other
parts of a program in java global and local scope are
defined by a class and by method
- As a general rule variable declared inside a scope are
not visible that is exsible to code that is define outside
the code .
-scope rules provide the foundation for encapsulation -
-scope can be nested .
EXAMPLE :
//demonstrate block scope
Class scope
{
Public static void main (String args [])
{
Int x ;
X =10;
If (x==10)
{
Int y=20;
Sysyem.out.println(“x and y “+x+ “ ”+y);
X = y *2;
}
System.out.println(“x is “+x );
}
}

#operators :
-Operators are used to perform operations on variables and values.

[1] Arithmetic operator :


-java arithmetic operator are used to perform addition ,
substraction , multiplication , and division .
-they act as basic mathematical operations .

EXAMPLE :
Public class demo
{
Public static void main (String args [])
{
Int a =10;
Int b =5;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}
}

OUTPUT:
15
5
50
2
0

[2] increment and decrement :


Type Operator Meaning Example
Increment ++ Increment ++i Or i++
by 1
Decrement -- Decrement --k or k--
by 1
[3] relational operator :

- comparison operator are used to compare two values


(or variables)
Example :
Public class main
{
Public static void main (String args [])
{
Int a=5;
Int b=3;
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(a<b);
System.out.println(a<=b);
System.out.println(a>b);
System.out.println(a>=);
}
}
OUTPUT :
False
True
False
False
True
True

[ 4 ] Short Circuit Logical operators :


-The evalution of logical expression exits between
before complete evalution .
-short circuit evalution return result before complete
evalution of the expression .
-Short circuit evalution avoid unnecessary work and
leads to efficient processing

[ 5 ] Assignment operator :
-single equal sign (=) operator .
EXAMPLE :
Int x , y , z ;
x=y=z;
//set x,y and z to 100

[ 6 ] Shorthand Assignment :
-java defines 12 shorthand assignment operators that
combine assignement with 5 arithmetic operators .
(+=,-=,*=,/=,%=)
Example :
X+=2 means x=x+2
Y*=5 means y=y*5

[ 7 ] Type conversion in Assignment :


-java performs type conversion between campatiable
data types.
- Assignment of an integar value to a float types
element automatically .
-Automatic conversion can be done only when target
data type is larger than source data type .
Example :
Int a = 5; long b; b=a;

[8] Casting Incompatible types :


- casting incompatible data type is explicit conversion.
Example :
//type casting

Public class typecasting


{
Public static void main (String args[])
{
double x;
int y;
x=25.50;
System.out.println(“value of [double]x :”+x);
System.out.println(“conversion of double to integer :”);
y=(int)x;
System.out.println(“value of [integer]y:”+y);
Int m;
double n;
m=10;
n=m;
System.out.println(“value of[integer]m:”+m);
System.out.println(“conversion of integer to double”);
System.out.println(“value of[double]n:”+n);
}
}

OUTPUT:

# operator precedence :
-precedence relation specifies which operation must be
done first during the expression evalution .
-Associativity tells the direction of execution of
operators that can be either left to right or right to left
Example :
In expression a=b=c=10; the assignment operator is
executed from right to left i.e. c will be assign 10; then
60 will be assigned by c and finally a will be assigned by
b.

#Expression :
-it is a combination of operators and operands in a
specific manner
EXAMPLE :
C = a+b*c ; // expression with arithmetic operators
(a>b)&&(b<c) // expression with logical operator and
relational operator .
-Evaluation of expression is a order of evaluating the
expression using precedence and associativity rules .
Example :
To evaluate 2+3*4 first 3*4=12 then 2 is added and the
result is 14 as multiplication has higher precedence
then addition .

YOUR CODE COACH

You might also like