You are on page 1of 36

Java :

 High level programming language introduced in the year 1995.


 Introduced by James Goshling with the help of Sunmicro Systems.

Edition :

 J2SE – Java standard edition


 J2EE – Java enterprise edition
 J2ME – Java mirco edition

J2SE can be used for standard application (notepad, paint)

J2EE (webapp, google, gmail)

J2ME (android application)

 Java has various version ( from 1.0 to 10 ) presently using Java 7 version
 Java software (JDK) JAVA DEVELOPMENT KIT. JDK is open source software without JDK we
can’t run java application

JVM ( Java virtual machine )

JRE ( Java runtime environment )

How to write a JAVA program :

JAVA program should start with class

Class

Class demo

Class name

{ method

Public static void main( string [] args)

{ statement

System.out.println(“ hello “);

To save program name.java

JDK – 8th version we are using


Steps :

CMD:

C:\User\java.S…….\Desktop>cd java

Javac Demo Java

Javac.java demo

“hello”

Key words :

 Java has 53 Key words

1. abstract -Specifies that a class or method will be implemented later, in a


subclass
2. assert -Assert describes a predicate (a true–false statement) placed in a Java
program to indicate that the developer thinks that the predicate is always true at
that place. If an assertion evaluates to false at run-time, an assertion failure
results, which typically causes execution to abort.
3. boolean – A data type that can hold True and False values only
4. break – A control statement for breaking out of loops
5. byte – A data type that can hold 8-bit data values
6. case – Used in switch statements to mark blocks of text
7. catch – Catches exceptions generated by try statements
8. char – A data type that can hold unsigned 16-bit Unicode characters
9. class -Declares a new class
10. continue -Sends control back outside a loop
11. default -Specifies the default block of code in a switch statement
12. do -Starts a do-while loop
13. double – A data type that can hold 64-bit floating-point numbers
14. else – Indicates alternative branches in an if statement
15. enum – A Java keyword used to declare an enumerated type. Enumerations
extend the base class.
16. extends -Indicates that a class is derived from another class or interface
17. final -Indicates that a variable holds a constant value or that a method will not
be overridden
18. finally -Indicates a block of code in a try-catch structure that will always be
executed
19. float -A data type that holds a 32-bit floating-point number
20. for -Used to start a for loop
21. if -Tests a true/false expression and branches accordingly
22. implements -Specifies that a class implements an interface
23. import -References other classes
24. instanceof -Indicates whether an object is an instance of a specific class or
implements an interface
25. int – A data type that can hold a 32-bit signed integer
26. interface – Declares an interface
27. long – A data type that holds a 64-bit integer
28. native -Specifies that a method is implemented with native (platform-specific)
code
29. new – Creates new objects
30. null -Indicates that a reference does not refer to anything
31. package – Declares a Java package
32. private -An access specifier indicating that a method or variable may be
accessed only in the class it’s declared in
33. protected – An access specifier indicating that a method or variable may only
be accessed in the class it’s declared in (or a subclass of the class it’s
declared in or other classes in the same package)
34. public – An access specifier used for classes, interfaces, methods, and
variables indicating that an item is accessible throughout the application (or
where the class that defines it is accessible)
35. return -Sends control and possibly a return value back from a called method
36. short – A data type that can hold a 16-bit integer
37. static -Indicates that a variable or method is a class method (rather than
being limited to one particular object)
38. strictfp – A Java keyword used to restrict the precision and rounding of
floating point calculations to ensure portability.
39. super – Refers to a class’s base class (used in a method or class constructor)
40.switch -A statement that executes code based on a test value
41.synchronized -Specifies critical sections or methods in multithreaded code
42. this -Refers to the current object in a method or constructor
43. throw – Creates an exception
44. throws -Indicates what exceptions may be thrown by a method
45. transient -Specifies that a variable is not part of an object’s persistent state
46 try -Starts a block of code that will be tested for exceptions
47. void -Specifies that a method does not have a return value
48. volatile -Indicates that a variable may change asynchronously
49. while -Starts a while loop

** The keywords const and goto are reserved, even they are not currently in use.
 const -Reserved for future use
 goto – Reserved for future use

Datatypes :

 Primitive datatypes
 Non primitive datatypes

Primitive datatypes :

 Boolean
 Char
 Bit
 Short =2 byte
 Int =4 byte
 Long =8 byte
 Double
 Float

Boolean ( To check expression true or false )

Char ( For representing character value )

Bit
Short
Long ( To represent integer values )
Int

Float
Double ( To represent the decimal values )

1 byte = 8 bits = 2^8 = 256


Short = 16 bits = 2^16 = 65536
Int = 32 bits = 2^32 = 4294967296

Class datatypes
{
Public static void main ( string [] arg)
{
Boolean b=true;
System.out.println(b);
Char c=’a’;
System.out.println(c);
Int a =10;
System.out.println(a);
Double d=10.333;
System.out.println(d);
}
}

 Path
 Desktop
 Cd java
 Javac desktop.java
 Java datatypes

To copy path :

 My computer
 C drive
 Program files
 Bin

For Boolean and char = no scanf function

For scanner :

 Import java.util.scanner ;

Syntax for scanner :

Scanner s=new.scanner(system.in);
system.out.println(“ enter the value );
int a=s.nextInt();
system.out.println(“ enter the value again “);
int d=s.nextInt();
int c=a+d;
system.out.println(c);

JAVA - CORE JAVA


J2EE - ADVANACED JAVA

CORE JAVA : SOFTWARE JDC

 Keywords, datatypes
 Variables and types
 Decision making statements
 Oops concept
 Exception handling
 Collection fragments
 Multi threading
 File.io
 Inner class

ADVANCED JAVA :

 SqL
 JDKC
 HTML

FOR JAVA CODING :

 IDE ( integrated development environment )


 Eclipse
 Netbeans
 Notepad
Keywords, Variables, Datatypes :

 To call function, keywords is used


 There are 53 keywords used for programming

Datatypes :

 Primitive datatypes
 Non primitive datatypes

To start java we should write with class and class name

Class demo

Class class name

Every program is written inside the class


Variables is the name given to the memory allocation

( eg )

Class demo
{
Public static void main ( string [] arg)
{
Byte b=45;
System.out.println(b);
}
}
Output :
15

Boolean are internal datatypes and others are external datatypes.

Another JAVA IDE: Eclipise


Instead of notepad and command promt we can use eclipse.

Steps :
 File -> new -> project -> program name
 Expand project name
Select source -> new -> package
[ package starts with com.demo]
 Next -> new -> class -> class name

17/07/2019

Decision making statements

If, else, else if, switch Looping, while, do while

Control statements :

If statement:

If the given condition is true “ if condition “ will execute.

If the given condition is false “ else condition “ will execute.


1.PROGRAM

package com.odd;

public class ageverify {

public static void main(String[] args) {

int age=89;

if(age>=18)

{
System.out.println(" eligible ");
}

else

{
System.out.println("not eligible ");
}

Output:

not eligible

Else if statement:

To execute multiple conditions “ else if condition “ is used.

2.PROGRAM

package com.odd;

public class mark {

public static void main(String[] args) {

int mark=85;

if(mark<35 && mark>0)


{
System.out.println(" fail");
}
else if(mark<=60 && mark>=35)
{
System.out.println(" grade C");
}

else if(mark<=80 && mark>=65)


{
System.out.println(" grade B");

else if(mark<=100 && mark>=80)


{
System.out.println(" grade A");
}

}}

Output :

Grade B

3.Even or odd Program :

package com.odd;

public class Oddoreven {

public static void main(String[] args) {

int number=15;
if(number%2==0)
{
System.out.println("even");

}
else
{
System.out.println("odd");
}
}

Output :

Odd
4.To find the greatest of number Program
package com.odd;

public class greatestofnumbers {

public static void main(String[] args) {

int x=95;
int y=75;
int z=83;

if(x>y && x>z)


{
System.out.println(" x is greater ");
}

else if(y>x && y>z)


{
System.out.println("y is greater ");
}
else if(z>x && z>y)
{
System.out.println(" y is greatest");
}
}

Output :

Z is greatest

5.Bodmas Program

package com.odd;

public class bodmas {

public static void main(String[] args) {

{
int a=10;
int b=20;
int c=2;
int d=25;
int e=15;
float z=d+b-c*a/e;
System.out.println(z);
{
System.out.println( a/e);
}
{
System.out.println(c*a);
}
{
System.out.println(d+b);

}
{
System.out.println(b-c);
}
{
System.out.println(10/15*2+25-20);
{
System.out.println(25+20-2*10/15);
}
}

Output :

19/07/2019

Looping statements :

1. While loop
2. Do while loop
3. For loop

While loop :

 If the condition is true the loop will execute.


 In false condition the loop will not execute.
Program :

package looping;

public class dowhile {

public static void main(String[] args) {


int a=0;
while(a<=100)
{
System.out.println(a);
a++;
}
}
}

Output :

1,2,3,…..,100

Do while loop :

 If the condition is true the loop will execute.


 If the condition is false the loop will execute the input value (eg int a=10) 10 is the output
which is executed.

Program :

package looping;

public class dowhile {

public static void main(String[] args) {


int a=20;
do
{
System.out.println(a);
a++;
}
while(a>=100) ;
}
}

Output :

20

The above condition is false (a>=20) where a = 10.


Program :

package looping;

public class dowhile {

public static void main(String[] args) {


int a=20;
do
{
System.out.println(a);
a++;
}
while(a<=100) ;
}
}

Output :

20,21,22,……,100

Decrement operator :

package looping;

public class dowhile {

public static void main(String[] args) {


int a=100;
do
{
System.out.println(a);
a--;
}
while(a>=20) ;
}
}

Output :

100,99,98,………,10
22/07/19

For loop :

 Command is given in a single line of code.

Program :
package looping;

public class forloop {

public static void main(String[] args) {


for (int a=2;a<=10;a++)
{
System.out.println(a);
}

Output :

0,1,2,…..,10

Notes :

 To increment a number the int value should be lesser then the assigned value and the
relational greater symbol should be on the assigned value.
 To decrement a number the int value should be greater than the assigned value and the
relation greater symbol should be on the int value.

Switch :

 Switch is used for selecting choices.

Program :

package looping;
public class Switch {

public static void main(String[] args) {

int options=3;
switch(options)
{
case 1:
System.out.println("breakfast");
break;
case 2:
System.out.println("lucnh");
break;
case 3:
System.out.println("snacks");
break;
case 4:
System.out.println("dinner");

}
}

Output :

Lunch

22/07/2019

Nested loop :

 For loop inside a for loop is called nested loop.


 Inner loop ( column )
 Outer loop ( row )

Program :
package examples;

public class nessloop {

public static void main(String[] args)


{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(j);

}
System.out.println();
}

Output :

00000

11111

22222

33333

44444

Program : Logic :

For ( int i=0;i<=5;i++) i=0 0<=0 * condition T

{ i=0 1<=0 condition F

For ( int j=0;j<=I;j++) i becomes 1 0<=1 * condition T

{ i=1 1<=1 ** condition T

System.out.print(*); i=1 2<=1 condition F

} i becomes 2 0<=2 “” T

System.out.println( ); i=2 1<=2 “” T

}
package examples;

public class asscnding {

public static void main(String[] args) {


for ( int i=0;i<=5;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(*);
}
System.out.println();
}

Output :

**

***

****

*****

Program :
package examples;

public class tables {

public static void main(String[] args) {


int i=1;
while(i<=20)
{
System.out.println("2x"+i+"="+(i*2));
i++;
}
}

Output :

2x1=2
2x2=4

2x3=6

……..

2x20=40

Program :

package examples;

public class all_reminders_ {

public static void main(String[] args) {


int i=1;
while(i<=1000)
{
if ((i%2==0) && (i%4==0) && (i%5==0) && (i%7==0));
{
System.out.println(i);
}
i++;
}
}

Output :

994
995
996
997
998
999
1000

Program :

package examples;

public class prime {

public static void main(String[] args) {


int x=27, count=0,i=7;
while ( i<=x)
{
if (x%i<=0)
count++;
i++;
}
if ( count==2)
{
System.out.println("is a prime number ");
}
else
{
System.out.println(" is not prime number ");
}

output :

Is a prime number

23/07/19

Arrays :

An array, in the context of java is dynamically created object that serves as a container to hold
constant number of values of the same datatype. By declaring an array, memory space is allocated
for values of a particular type.

Program :

package arrays;

public class array3 {

public static void main(String[] args) {


int a[]=
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
}
}

Output :

1234567891011121314151617

Program :

package arrays;

public class array2 {

public static void main(String[] args) {


int a[][]= {{1,2,3},{1,2,3}};
for(int i=0;i<2;i++) I is for row
{
for(int j=0;j<3;j++) j is for column
{
System.out.print(a[i][j]);

}
System.out.println();
}

Output :

123

123

Program :

package arrays;
public class arrays {

public static void main(String[] args) {


int a[] = new int [8];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
a[5]=60;
for(int i=0;i<6;i++)
{
System.out.println(a[i]);
}

Output :

10

20

30

40

50

60

Program :

package arrays;

import java.util.Arrays;

public class array5 {

public static void main(String[] args) {


int a[]= {4,7,1,3,5,6,8,-3,-5};
Arrays.sort(a);
System.out.println(Arrays.toString(a));

int z=Arrays.binarySearch(a,-5);
System.out.println(z);

Output :

-5-3,1,3,4,5,6,7,8

Program :

package arrays;

public class array4 {

public static void main(String[] args) {


int a[][]= {{1,5,6},{2,5,7}};
int b[][]= {{2,3,4},{5,6,7}};
int c[][]=new int[2][3];

for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}

Output :

3 8 10
7 11 14
24/07/19
String :

 String is a collection of characters .


 String is a non primitive data type as well as class .
 String contains in the language package Java.lang-string.
 String is an immutable object once the object is created it cannot be changed.

(eg):

Pool heep

1 s1 } ->
2 s2 }
3 s3 }

Scanner function :

Scanner is a class in java.util package used for obtaining the input of the primitive types like int,
double, etc. and strings. It is the easiest way to read input in a java program, through not very
efficient in your want an input method for scenarios where time is a constraint like in competitive
programming.

Scanner s=new Scanner (System.in);


System.out.println("enter the value ");
int a=s.nextInt();
System.out.print("enter the b value ");
int b=s.nextInt();
int c=a+b;
System.out.prinln(c);

Program :

package string;

import java.util.Scanner;

public class string_2 {

public static void main(String[] args) {


Scanner s=new Scanner (System.in);
System.out.println("enter the size of an array ");
int a=s.nextInt();
int n[]=new int[a];
System.out.println("enter the elements of array :");
for (int i=0;i<a;i++)
{
n[i]=s.nextInt();
System.out.println(n[i]);
}

Output :

enter the size of an array


2
enter the elements of array :
1
1
2
2

Program :

package string;

public class string3 {

public static void main(String[] args) {


String s="sai";

String s1=new String("sai");

char c[]= {'j','a','v','a'};


String x=new String(c);
System.out.println(x);

Output :

java

Program :

package string;

public class string4 {

public static void main(String[] args) {


String s="vicky";
System.out.println(s.length());
System.out.println(s.charAt(0));
System.out.println(s.indexOf("v"));

String x="siva";
String y=x.concat("rama");
System.out.println(y);

Output :

5
v
0
sivarama

Program :

package string;

import java.util.Scanner;

public class string {

public static void main(String[] args)


{
Scanner s=new Scanner (System.in);
System.out.println("enter a value");
int a=s.nextInt();

System.out.println("ente b values ");


int b=s.nextInt();
int c=a+b;
System.out.println(c);

Output:

enter a value
2
ente b values
4
6
Program :

package string;

public class string_reverse {

public static void main(String[] args) {


String rev="vicky";
for(int i=rev.length()-1;i>=0;i--)
{
System.out.println(rev.charAt(i));
}

Output :

y
k
c
i
v

Program :

package string;
import java.util.Scanner;
public class enter_string {

public static void main(String[] args) {

Scanner s=new Scanner (System.in);


System.out.println(" enter string ");
String name=s.next();
System.out.println(name);
System.out.println(name.length());

Output :

enter string
vicky
vicky
5
Program :

package string;

public class string_buffer {

public static void main(String[] args) {


StringBuffer sb=new StringBuffer("vicky");
sb.append("falsh");
System.out.println(sb); // string concatination
System.out.println(sb.reverse()); // string reversing

Output :

vickyfalsh
hslafykciv

Program :

package string;

public class substring {

public static void main(String[] args) {


String s="rahul dravid";
System.out.println(s.substring(0,5));
}

Output :

rahul

Program :

package string;

public class debit_carad_substring {


public static void main(String[] args) {
String x="1234567890123456";
System.out.print("************"+x.substring(12,16));
}
}

Output :

************3456

Program :

package string;

public class string5 {

public static void main(String[] args) {


String a="sai";
String b="sai";
System.out.println(a==b);

String x=new String("sai");


String y=new String("sai");
System.out.println(x.equals(y));

Output :

true
true

Oops :
 Global variable is declared inside the class and outside the method.
 Int is a global variable that is declared inside the class and outside the
method.
 A obj = name A()

Class object method constructor

 Constructor is used for inheritance of object.


 Object is used to store a memory allocation.

Program :

package oops;

public class Demo {


String name="java";
public static void main (String[] args)
{
Demo d=new Demo();
System.out.println(d.name);

Output :

Java

Meathods :

 Is to be performed a specific task on operation.


 Code reusability.

Local variable :
 declared inside a variable is called local variable.
 Default value of global variable is 0
 Default value of global string is null
 Default value of global float is 0.0

Program :

package oops;

public class A {

int a;
int b;
int c;

void add()
{
a=10;
b=20;
c=a+b;
System.out.println(c);
}
void sub()
{

a=10;
b=20;
System.out.println(c);

}
A a=new(A);
a.add

Output :

Program :

package meathod;

public class College {

public void cse ()


{
System.out.println("Diviya")
;

}
public void ece()
{
System.out.println("magesh ");

}
public void mech()
{
System.out.println(" babu");

Main :

package meathod;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

System.out.println("Mennakshi College Of Enginerring ");


System.out.println("==================================");
System.out.println("Select your Choice ");
System.out.println("================================");
System.out.println("1.CSE");
System.out.println("2.ECE");
System.out.println("3.MECH");
System.out.println("================================");
Scanner s=new Scanner (System.in);
System.out.println("enter ur choice");
int choice=s.nextInt();

College c=new College ();


switch(choice) {
case 1:
c.cse();
String s1[]= {"A","M","B"};
for(String x:s1)
{
System.out.println(x);
}
break;

case 2:
c.ece();
String s2[]= {"B","v","X"};
for(String y:s2)
{
System.out.println(y);
}
break;
case 3:
c.mech();
String s3[]= {"c","f","s"};
for(String z:s3)
{
System.out.println(z);

}
}

}
}

Output :

Mennakshi College Of Enginerring


==================================
Select your Choice
================================
1.CSE
2.ECE
3.MECH
================================
enter ur choice
2
magesh
B
v
X

Program :

package meathod;

public class Cinema {

String name;
public void cinematography()
{
name="KV anadnh";
System.out.println(name);

}
public void direction ()
{
name="shaankar";
System.out.println(name);

public void music()


{
name="AR Rahaman";
System.out.println(name);
}
public void dance()
{
name="Prabhu Deva";
System.out.println(name);

}
}

Main : Object Creation

package meathod;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

System.out.println("Cinema ");
System.out.println("==================================");
System.out.println("Enthiran 2.0");
System.out.println("==================================");
System.out.println("1. Direction ");
System.out.println("==================================");
System.out.println("2. Music artist");
System.out.println("==================================");
System.out.println("3. Cinematography");
System.out.println("==================================");
System.out.println("4. Actor");
Scanner s=new Scanner (System.in);
System.out.println("enter ur choice");
int choice=s.nextInt();

Cinema c=new Cinema ();


switch(choice) {
case 1:
c.direction();
String s1[]= {};
for(String x:s1)
{
System.out.println(x);
}
break;

case 2:
c.music();
String s2[]= {};
for(String y:s2)
{
System.out.println(y);
}
break;
case 3:
c.cinematography();
String s3[]= {};
for(String z:s3)
{
System.out.println(z);
}
break ;
case 4:
c.actor();
String s4[]= {};
for(String w:s4)
{
System.out.println(w);

}
}

Output :

Cinema
==================================
Enthiran 2.0
==================================
1. Direction
==================================
2. Music artist
==================================
3. Cinematography
==================================
4. Actor
enter ur choice
2
AR Rahaman

Methods :

Methods

Instance Static

Parameter Non parameter parameter Non parameter

Program :
package meadthods1;

public class Class {


public void sectionA( int strength)

{
System.out.println(strength);

public void sectionB(int strength)


{
System.out.println(strength);

public static void main(String[] args) {

Class c=new Class();


c.sectionA(100);
c.sectionB(10);

Output :

100
10

Program :

package meadthods1;

public class Retrun


{

public int add()


{
int a=10;
return a;
}

public String name()


{
String v="vicky";
return v;

}
public static void main(String[] args)
{
Retrun r=new Retrun();
System.out.println(r.add());

Output :

10

Program :

package meadthods1;

public class V {

public static void add()


{
System.out.println("welcome");
}

public static void main(String[] args) {

V.add();

Output :

welcome

You might also like