CORE JAVA
March 28, 2017 | Author: Svsn Sandeep Svsn | Category: N/A
Short Description
Download CORE JAVA...
Description
1
CORE JAVA SunMicrosystem divided the Java into three groups: 1) J2SE(Java2 Standalone Edition): Deals with developing a standalone application.(Core Java, Adv.Java). 2) J2EE(Java2 Enterprise Edition): Deals with developing business application for Internet. 3) J2ME(Java2 Micro Edition): Deals with developing embedded system and wireless application. Java Program Execution: javac
x.java
x.class
JVM
Result
Byte code Features Of Java: 1) Simple: Java is simple programming language. Learning and practicing java is easy because its look like c,c++. Pointers are eliminated in java due to the following reason: i) Pointer creates confusion for a programmer. ii) Pointer may crash a program. If we can’t use pointers properly crash occurs. iii) Using pointers virus and hacking programs are developed. 2) Object Oriented: i) Object: An object is anything that exists in the real world. Object will have properties and it may perform some actions. Properties: Properties are represented as variables in java. Actions: Actions are represented as methods. An Object contains variables and methods. ii) Class: A class is a model to create the objects. Class does not exist physically. A class also contains properties and actions. 3) Distributed: Java is distributed programming language. 4) Robust: In Robust we have two features: i) Exception Handling ii) Memory Management Memory Mangement: Memory allocation is done by JVM. JVM contains class loader
2 subsystem to allot memory. Memory Deallocation is done by JVM Garbage collection. Garbage collection automatically removed unused objects. 5) Portable: Java is system independent. All java program are portable because it will give same result anywhere. 6) High Performance: JVM contains i) JIT compiler and ii) Interpreter JIT: Just in Time compiler is useful to increase the speed of the execution. Interpreter: Interpreter shows the line number. Demo Program: Demo.java import java.lang.*; class Demo { public static void main(String[] args) { System.out.println("Hello World!"); } } import: import is a keyword which is used to import the packages. java: java is a java library. lang: lang is the name of the default package. class: Class is a keyword to define class name. public: Public data available to all other programmer. static: static keyword is used to call main method without creating object. void: void is a return type. Main method doesn’t return any value but it will execute all the Statement. main: Name of the main method. String[] args: Here args is the name of the array. The type of the array is String. System: System is a class. out: Out is a static field. Static field can’t changed.
3 println: Name of the method. JVM Architecture: JVM is divided into five sections: 1) Method Area: It contains the class code and method code. 2) Heap: Objects are created on Heap memory. 3) Java Stack: Java Stack are the places where java methods are executed. 4) PC Register: These registers store memory address of next instruction to be executed by the microprocessor. 5) Native Method Stack: These are the places where native methods are store. Comments: Comments represents the description of aim and features of a program. The main advantage of comments are Readability. Readability means understandable by everyone. There are three types of comments in java. i) ii) iii)
Single line comment: This comment starts with //. Multiline comment: This comment starts with /* and ends with */. Documentation comment: This comment starts with /** and ends with */.
Q: What is an API document? A: (Application Programming Interface). It is a document containing description of all the features of software or product or technology. We can create our own API document by using javadoc command. Q: What is the difference between #include and import? A: #include makes the compiler to copy the entire header file code into a c or c++ program. Thus it increases the size of the program and waste memory and processor time. Import makes JVM to goto the java library, execute the code there, comes back and substitute the result into a java program. Q: What is JRE(Java Runtime Environment)? A: JRE=JVM + JavaLibrary Q: What is command line argument? A: The values pass to main method is called command line argument. The values store in String type array.
4 Naming Rule: 1) Package names in java are written in all small letters. Ex: java.lang.*,java.io.* etc 2) Each word of class name and interface name start with a capital letter. Ex: Thread, String 3) Method name start with a small letter than each word start with a capital letter. Ex: read(), readLine() etc 4) Variable names also follow the above method rule. Ex: String empName, int empNum etc. 5) Constant should be written using all capital letters. Ex: int final PI=22/7, MAX=10 etc. 6) All keywords should be written in all small letters. Ex: import, public, static etc. Data Type: A datatype represents the type of data stored in memory. Primitive Data Type or Fundamental Data Type: I) Integer Data Type Data Type i) byte ii) short iii) int iv) long
Memory Size 1byte 2 bytes 4 bytes 8 bytes
II) Float Datatype i) float ii) double
4 bytes 8 bytes
III) Character Datatype i) char 2 bytes IV) Boolean datatype i) boolean 1bit(true or false) Q: What is the difference between float and double? A: float can represent upto 7 digit after decimal point accurately. Double can represent upto 15 digit accurately after decimal point. Operator: An operator is a symbol that perform some operations. Ex: A + B (Here + is an operator and A,B is called operand) Operand is a variable in which the operator acts. An operator can act upon single operand than it is called “unary” operator. An operator can act upon 2 operand than it is called “binary” operator. If an operator act upon 3 operand than it is called “ternary” operator.
5 Operator in Java: 1) Arithmetic Operator:+,-, %, *, / 2) Unary Operator:++, -3) Assignment Operator: =, +=, -=, *=, %= 4) Relational Operator: , ==, != 5) Logical Operator: &&, ||, ! 6) Boolean Operator: &, |, ! 7) Bitwise Operators: a) Bitwise complement: ~ b) Bitwise and:& c) Bitwise or: |(pipe symbol) d) Bitwise XOR:^(cap symbol) 8) Dot Operator: a) To refer to a class into package ex: java.util.Date, java.io.BufferedReader b) To refer to a method of a class ex: Math.pow(), Emp.sum() c) To refer to a variable in a class ex: Emp.name, Emp.no Increment Operator: This operator increases the value of a variable by 1. We have 2 types in increment operator.Ex: ++x 1) Preincrementation: In Preincrementation , incrementation has done first and any other operation are performed next. Example: //Preincrementation example class Pre { public static void main(String[] args) { int x=7; System.out.println(++x); System.out.println(x); System.out.println(++x); System.out.println(x); } } 2) Postincrementation: In Postincrementation any other operation is done first, incrementation is performed at the end.Ex: x++ Example:
6 //postincrementation class Post { public static void main(String[] args) { int x=18; System.out.println(x++); System.out.println(x); System.out.println(x++); System.out.println(x); } } PrePost Example class PrePost { public static void main(String[] args) { int x=3; //System.out.println(++x*x++*x++); System.out.println(++x*x++*++x); System.out.println(x); } }
The precedence of java operators:-1) Increment and Decrement Operator 2) Arithmetic Operations 3) Comparisons 4) Logical Operations 5) Assignment Operations Highest () []
.
++
--
~
*
/
%
+
-
>>
>>>
>=
<
!
j ? i : j; // ternary operator(output is 55) System.out.println("The value assigned is " + z); } } Reverse: class Reverse { public static void main(String[] args) { int r=0,rev=0; int n=63674; while(n>0) { r=n%10; rev=(rev*10)+r; n=n/10; } System.out.println("The Reverse Number is-->"+rev); } } Control Statement: Executing the statement one by one is called Sequential Execution. Executing the statement randomly is called Random Execution. Random Execution is useful to write better program. Random Execution is possible by using control statement. Control statements are the statements which change the flow of execution of a program. 1) if-else statement: This statement is useful to perform a task depending upon whether a condition is true or false. Example 1: class IfTest { public static void main(String[] args) { int num=-4; if(num==0) { System.out.println("It is zero"); } else if(num>0) { System.out.println("It is positive");
9 } else { System.out.println("It is negative"); } } }
Example 2: //Nested if-else class IfElse { public static void main(String args[]) { int month = 4; String season; if(month == 12 || month == 1 || month == 2) { season = "Winter"; } else if(month == 3 || month == 4 || month == 5) { season = "Spring"; } else if(month == 6 || month == 7 || month == 8) { season = "Summer"; } else if(month == 9 || month == 10 || month == 11) { season = "Autumn"; } else { season = "Bogus Month"; } System.out.println("April is in the " + season ); } }
2) do-while loop: This loop is used to repeat early execute a group of statements as long as
10 given condition is true. Example: class DoWhileTest { public static void main(String[] args) { int x=0; do { System.out.println(x); x++; } while(x>=10); } } 3) while loop: This loop is repeatedly execute a group of statements as long as given condition is true. Example: class WhileTest { public static void main(String[] args) { int i=2; while(true) //while(i
View more...
Comments