Docsity

Notes core java for computer science engineering students, Lecture notes of Java Programming

Toggle sidebar
bg1
Module 1: Core Java Programming
Section 1: Basics of Programming:
1. Java Components (javac, jvm, jre, jdk, and Installation)
2. Sample program
• Compilation
• Execution
3. Variable and Operators.
4. Functions/Methods
5. Control Statements
• If
• Switch
• Looping
6. Array
7. Strings
Section 2: OOPS:
8. Class and Object
9. Initialization block
10.Constructors
11.Composition
12.Inheritance
13.Method Overloading/Method Overriding
14.Typecasting
15.Polymorphism
16.Abstract Class
17.Interface
18.Abstraction
19.Encapsulation
20.JavaBean Class
21.Singleton Class
Section 3: Java libraries:
22.Object Class function
23.In-depth of Strings
24.In-depth of Arrays
25.Exception Handling
26.Multithreaded Programming
27.Collection Framework libraries
28.File Programming
Module 2: J2EE (Java 2 Enterprise Edition)
Section 1: JDBC Programming:
Pre-Condition You need to completed Java and Sql
1. Connect to DB
2. Insert
3. Update
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62

Partial preview of the text

Download Notes core java for computer science engineering students and more Lecture notes Java Programming in PDF only on Docsity!

Module 1: Core Java Programming

Section 1: Basics of Programming:

  1. Java Components (javac, jvm, jre, jdk, and Installation)
  2. Sample program
    • Compilation
    • Execution
  3. Variable and Operators.
  4. Functions/Methods
  5. Control Statements
    • If
    • Switch
    • Looping
  6. Array
  7. Strings

Section 2: OOPS:

  1. Class and Object
  2. Initialization block 10.Constructors 11.Composition 12.Inheritance 13.Method Overloading/Method Overriding 14.Typecasting 15.Polymorphism 16.Abstract Class 17.Interface 18.Abstraction 19.Encapsulation 20.JavaBean Class 21.Singleton Class

Section 3: Java libraries: 22.Object Class function 23.In-depth of Strings 24.In-depth of Arrays 25.Exception Handling 26.Multithreaded Programming 27.Collection Framework libraries 28.File Programming

Module 2: J2EE (Java 2 Enterprise Edition)

Section 1: JDBC Programming:

Pre-Condition You need to completed Java and Sql

  1. Connect to DB
  2. Insert
  3. Update
  1. Delete

Section 2: Servlets Programming: Pre-Condition You need to completed Java+Sql+JDBC and web Technologies (HTML,CSS,JavaScript)

  1. Develop Application which can Run on Servers

Section 3: JSP Programming

Module 3: Frameworks

  1. MVC Architecture
  2. Springs
  3. Hibernate ================================================== =================================== 15 June 2016 ================================================== =================================== Java Components

Java Program

J

S

JRE

Java components explanation: A programmer writes a statements in java editor using high-level language and saves it with an extension of filename.java

Java Statement s

(English)

Java Compiler

(Javac)

*Syntax

Byte code

Filename.cl ass

Executable

JIT: Just in Time (Compiler)InterpreterExecutionResult

--------------- //Statements } //End of Main Method Body Body } //End of Class body

OCJP: Oracle certified Java Professionals (Java Certification cost 8k) The folder structure need to be maintained

Javac filename.java : compilation command Java classname : Execution command

==================================================

class Program { public static void main(String[] args) { System.out.println('H'); } }

Output : H

  1. Use cmd prompt to change the drive using :
  2. Then compile using the javac filename command
  3. Execute the program using the java classname command ================================================== =================================== class Program { public static void main(String[] args) { System.out.println(12544); System.out.println(12.444); System.out.println('j'); System.out.println(true); System.out.println("Sunil"); } } ================================================== ===================================

class Program { public static void main(String[] args) { System.out.println('H'); System.out.println('i'); } }

//This program output will have 3lines first line will print H and second will print i and third line will be empty as the cursor will move to next line ================================================== ===================================

Println: Prints the statement and moves cursor to next line so in the last program we got three-line o/p Print: Prints the statement and move to next char in same line

class Program { public static void main(String[] args) { System.out.print('H'); System.out.print('i'); System.out.println();//Moves cursor to next line System.out.println("Execution Ended"); } } ================================================== =================================== + Operator: The + operator performs two roles

  1. Addition
  2. (^) Concatenation ================================================== =================================== class Program { public static void main(String[] args) { System.out.println(20+20); System.out.print("JAVA "+"Developer"); } } ================================================== =================================== Association of operator is from left to right ================================================== ===================================

class Program { public static void main(String[] args) {

Int Long

Float //used to store numeric floating point numbers Double

Char //used to store character

Boolean //use to store true/false

Variable initialization: Variable initialization syntax Variablename=value; Eg x=12; y=4.5; i=’k’; j=true;

Note : without initializing we will not be able to use the variable in java. class Program { public static void main(String[] args) { //Variable Declaration and Initialization int x=12; float y=4.5f; char j='S'; boolean i=true; System.out.println("Value of x is "+x); System.out.println("Value of y is "+y); System.out.println("Value of j is "+j); System.out.println("Value of i is "+i); } }

Note: in java language it is mandatory to initialize the variable before we use it in any operation otherwise the java compiler throws error

class Program { public static void main(String[] args) { //Variable Declaration and Initialization int x1=12; System.out.println("Value of x is "+x1); x1=24; //re assignment System.out.println("Value of x is "+x1); x1=45;//re assignment System.out.println("Value of x is "+x1);

class Program { public static void main(String[] args) { //This is the program to get compile time error final int x=123; //if we use final we can assign value for final variable only once System.out.println("Value of x is "+x); x=24; //re assignment so that it will give compilation error System.out.println("Value of x is "+x); } }

class test { static int x1=12; } class Program { public static void main(String[] args) { //This is the program to copy value from one variable to other test.x1=12; int x2=34; int x3=56; System.out.println("Value of x1 is "+test.x1); System.out.println("Value of x2 is "+x2); System.out.println("Value of x3 is "+x3); x2=test.x1;//copy value f x1 to x x3=x2;//copy value f x2 to x //System.out.println("Value of x1 is "+test.x1); System.out.println("Value of x2 is "+x2); System.out.println("Value of x3 is "+x3); } }

Java language provides a keyword by name “final” whenever a variable is declared by using final keyword we can initialize the variable only once if we try to re- initialize then the compiler throws error

class Program { public static void main(String[] args) { //When you are trying to declate float or long we need to suffic with f an l

class Program { public static void main(String[] args) { int num=5; System.out.println("Programn starts"); if (num==5) { System.out.println("Given number is 5"); } System.out.println("Programn Ends"); } }

If else Syntax If (Condition) { //Execute the statements1. } else { //Execute the statements2. }

Eg: class Program { public static void main(String[] args) { int age=17; System.out.println("Programn starts"); if (age>=18) { System.out.println("Eligible to Vote"); } else { System.out.println("Not Eligible to Vote"); } System.out.println("Programn Ends"); } }

If elseif Syntax If (Condition) { //Execute the statements1. } elseif(Condition)

//Execute the statements2. } else { //Execute the statements3. }

Eg: class Program { public static void main(String[] args) { int num=6 ; System.out.println("*****Program starts*****"); if (num==5) { System.out.println("Given number is "+num); } else if (num==7) { System.out.println("Given number is "+num); } else System.out.println("Given number is Neither5 Nor7 "); System.out.println("*****Program Ends*****");

} }

Switch Syntax Switch (Condition) { Case 1://Execute the statements1. Break; Case 2://Execute the statements2. Break; Case 3://Execute the statements3. Break; Case 4://Execute the statements4. Break; }

Eg: class Program { public static void main(String[] args) { int Marks=70;

  1. if it enters to infinite loop to terminate it, we need to use break; in if statement

do while loop syntax do { //statements Increment/decrement } While(condition);

Execution flow

  1. Statements get executed first
  2. Condition check happens only at end
  3. We need to terminate the condition For examples check the loop control statements program in laptop

==================================================

class Program { public static void main(String[] args) { for(int i=1; i<=5;i++) { System.out.print(i); } System.out.print("\n Thankyou"); } } ================================================== ================================== class Program { public static void main(String[] args) { for(int i=5; i>=1;i--) { System.out.print(i); } System.out.print("\nThankyou"); } } ================================================== ================================== class Program //Nested for loop { public static void main(String[] args) {

for(int k=1; k<=5;k++) { System.out.println("Running outer loop-"+k);

for(int i=1; i<=5;i++) { System.out.println("\tRunning inner loop-"+i); } } } } ================================================== ================================== class Program //Nested for loop { public static void main(String[] args) { for(int k=1; k<=5;k++) { for(int i=1; i<=5;i++) { System.out.print(i); } System.out.println(); } } } ================================================== ================================== class Program //Nested for loop { public static void main(String[] args) { for(int k=1; k<=5;k++) { for(int i=5; i>=1;i--) { System.out.print(i); } System.out.println(); } } } ================================================== ================================== class Program //Nested for loop {

public static void main(String[] args) { for(int k=1; k<=5;k++) { for(int i=1; i<=k;i++) { System.out.print(k); } System.out.println(); } } } ================================================== ================================== class Program // Neted loop { public static void main(String[] args) { for(int k=5; k>=1;k--) { for(int i=1; i<=k;i++) { System.out.print(i); } System.out.println(); } } } ================================================== ================================== class Program // Nested loop { public static void main(String[] args) { for(int k=5; k>=1;k--) { for(int i=1; i<=k;i++) { System.out.print(k); } System.out.println(); } } } ================================================== ================================== class Program

// Nested loop important for interview to check for syntax and breaking for from infinite { public static void main(String[] args) { int i=1; for(;;) { System.out.print(i); i++; if (i==10) { break; } } } } ================================================== ================================== class Program // while looping { public static void main(String[] args) { int i=1; while (i<=5) { System.out.print(i); i++; } } } ================================================== ================================== class Program // while looping Assignment using while loop //1.User withdraw of rupees 2000 continously //2.once the balance reaches withdraw (5000) the loop to be terminated { public static void main(String[] args) { int bal=100000; int withdraw=2000; int minbal=5000; while ((bal-withdraw)>minbal) { bal=(bal-withdraw); System.out.println(bal); }

// Methods or Functions //Main method is the starting point of execution //can we write program without main method: yes //can we compile a program without main method: yes //can we execute a program without main method: No

class Program { static void test() { System.out.println("Running test() method.."); } //Main Method public static void main(String[] args) { //JVM starts execution here System.out.println("Execution started"); test(); //calling test() test(); //calling test() System.out.println("Execution ended"); }//JVM Ends execution here } ================================================== =================================== // Methods or Functions class Program { static void test() { System.out.println("Running test() method.."); demo();//call to demo() method System.out.println("Finish test() method.."); } static void demo() { System.out.println("Running demo() method.."); } //Main Method public static void main(String[] args) {//JVM starts execution here System.out.println("Execution started"); test(); //calling test() System.out.println("Execution ended"); }//JVM Ends execution here } ================================================== =================================== // Methods or Functions class Program { static void square(int n)

System.out.println("calculating square of "+n); int res; res=nn; System.out.println("Square is "+res); } //Main Method public static void main(String[] args) {//JVM starts execution here System.out.println("Execution started"); square(5); //when you are calling a function without initializing you cannot call square(9); square(283); System.out.println("Execution ended"); }//JVM Ends execution here } ================================================== =================================== // Methods or Functions class Program { static void areaOfTriangle(double base, double height) { System.out.println("calculating area of triangle"); System.out.println("\tBase is "+base); System.out.println("\tHeight is"+height); double a1; a1=0.5base*height; System.out.println("\tArea is "+a1); System.out.println("****************"); } //Main Method public static void main(String[] args) {//JVM starts execution here System.out.println("Execution started"); areaOfTriangle(5.0,8.0); areaOfTriangle(25.0,87.0); System.out.println("Execution ended"); }//JVM Ends execution here } ================================================== =================================== // Methods or Functions //in this example the return user can take it or he can ignore depends on user class Program { static double waiter(double amt) { System.out.println("Paying the bill"); double change; double billAmt=1400.0;