Beruflich Dokumente
Kultur Dokumente
Java - Overview
Java programming language was originally developed by Sun
MicroSystems which was initiated by James Gosling and released in 1995 as
core component of 'Sun Microsystem' Java Platform (Java 1.0 [J2SE]).
• The latest release of the Standard edition is JAVA SE 8.
• The new J2 versions were renamed as Java SE, Java EE and Java ME
respectively. Java is guaranteed to be Write Once, Run Anywhere.
Java is :
• Object Oriented: In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
• Platform Independent: Unlike many other programming language
including C and C++ , when java is compiled, It is not compiled into
platform specific machine, rather into platform independent byte code.
This byte code is distributed over the web and interepted by the Virtual
Machine (JVM) on whichever platform it is being run on.
• Simple: Java is designed to be easy to learn. If you understand the basic
concept of OOP Java, it would be easy to master.
• Secure: With Java`s secure feature it enables to develop virus-free,
tamper-free systems. Authentication techniques are based on Public-key
encryption.
• Architecture-neutral: Java compiler generates an architecture-neutral
object file format, which makes the compiles code executable on many
processors,with the presence of Java runtime system.
• Portable: Being architecture-neutral and having no implementation
dependent aspects of the specification makes java portable.
• Multithreaded: With Java`s multithreaded feature it is possible to write
programs that can perform many tasks simultaneously.
Page 1
Core java
Page 2
Core java
• Now, alter the 'Path' variable so that it also contains the path to the Java
executable. Example, if the path is curretly set to
'C:\WINDOWS\SYSTEM32', then change your path to read
'C:\WINDOWS\SYSTEM32;c:\ Program Files\java\jdk\bin';
• Type javac in cmd to check whether java is installed or not
• Type java -version is to check whether which versions it is
Java Editions:
• Java SE (Standard Edition)
Use to develop standalone applications
• Java EE (Enterprise Edition)
Use to develop web applications
• Java ME (Micro Edition)
Use to develop Mobile Applications
class class_name
{
public static void main(String[ ] args)
{
//statement or code to execute
}
}
Page 3
Core java
Let's look at how to save the file, compile and run the program.
• Please follow the steps given below: Open notepad and add the code as
above.
• Save the file as: MyFirstJavaProgram.java.
• Open a command prompt window and go o the directory where you saved
the class. Assume it's D:\ Type 'javac MyFirstJavaProgram.java ' and
press enter to compile your code.
• If there are no errors in your code, the command prompt will take you to
the next line(Assumption : The path variable is set).
• Now, type ' java MyFirstJavaProgram ' to run your program.
• You will be able to see ' Hello World ' printed on the window.
• D :> javac MyFirstJavaProgram.java
• D:> java MyFirstJavaProgram
HelloWorld
Page 4
Core java
Page 5
Core java
1. In Source code, creation step the java program is created and saved in a
file to the extension .java , this file contain only java statements.
2. In Compilation stage, the source code is converted into byte code by
java compiler. The java compiler translates the java statements into byte code
statements and save it in a file with a extension .class
The .class file is generated by java compiler. The byte code statements is
not in a machine executable formats.
3. In Execution stage, JVM executes the byte code and gives the result of
the byte code statement. While executing the byte code the JVM translates the
byte code into machine executable formats with the help of JIT compiler.
The output of the JIT compiler is interpreted by the JVM to give the
result of Program. The .class file can be executed on any operating system
Page 6
Core java
Page 7
Core java
Example
class Program
{
public static void main(String[] args)
{
System.out.println("JAVA");
System.out.println("J2EE");
}
}
E:\>cd core_java
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
JAVA
J2EE
E:\Core_Java>
• println( ) it will print the value provided and cursor will go to the next
line.
• print( ) it will also print the value provided and cursor will remain in the
same line.
Example:
class Program
{
public static void main(String[] args)
{
System.out.println("Hello");
Page 8
Core java
System.out.print("World");
System.out.println("1234");
System.out.print("abcd");
}
}
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
Hello
World1234
abcd
E:\Core_Java>
Keywords in Java :
• Keywords are Pre-defined reserved words which has already have the
certain meaning in java.
• Total, we have 50 keywords available in java.
Example: public, static, int , abstract ,double................
• Keywords cannot be used as your identifiers in java.
Identifiers:
• Identifiers are the name provided by the programmer.
Example: Class_name, method_name, variable_name....
Rules
1. We can have alphanumeric values or alphabets.
Eg: abc123
abc
2. We can have combination of special characters.
Eg: abc@def123
3. We can`t start with numeric value.
Eg: 123 ----is not allowed
Page 9
Core java
Datatypes:
There are eight primitive data types supported by Java. Primitive data
types are predefined by the language and named by a keyword.
byte:
Byte data type is an 8-bit signed two's complement integer.
Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
Example: byte a = 100, byte b = -50;
short:
Short data type is a 16-bit signed two's complement integer.
Short data type can also be used to save memory as byte data type, short is 2
times smaller than an int.
Example: short s= 10000, short r = -20000;
int:
int data type is a 32-bit signed two's complement integer.
Int is generally used as the default data type for integral values unless there is a
concern about memory.
Example: int a = 100000, int b = -200000;
long:
Long data type is a 64-bit signed two's complement integer.
This type is used when a wider range than int is needed.
Example: int a = 100000L, int b = -200000L ;
float:
Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point numbers.
Example: float f1 = 234.5f;
Page 10
Core java
double:
double data type is a double-precision 64-bit IEEE 754 floating point.
This data type is generally used as the default data type for decimal values,
generally the default choice.
Example: double d1 = 123.4;
boolean:
boolean data type represents one bit of information.
There are only two possible values: true and false.
char:
char data type is a single 16-bit Unicode character.
Char data type is used to store any character.
Example: char letterA ='A';
Variables:
• Used to store an value.
• Used to respresent an datatype.
Variable Declaration
Syntax :
Datatype variable_name ;
Example : int a;
Variable Initialization
Syntax :
Datatype variable_name = value;
Example : int a = 10;
Page 11
Core java
Example :
class Program
{
public static void main(String[] args)
{
int a=21;
float b=5.5f; // f is compulsory
double c=12345;
long d=1234l;// l is not compulsory
char e='a';
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
21
5.5
12345.0
1234
a
E:\Core_Java>
Page 12
Core java
E:\Core_Java>javac Program.java
Page 13
Core java
Concatenation
By + (string concatenation) operator
Example :
class TestConcatenation1
{
public static void main(String args[])
{
System.out.println("Sachin"+" Tendulkar";);
}
}
Output
Sachin Tendulkar
Example :
class Program
{
public static void main(String args[])
{
System.out.println(50+30+"PMKK"+40+40);
}
}
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
80PMKK4040
E:\Core_Java>
Page 14
Core java
Basic Operators
Java provides a rich set of operators to manipulate variables. We can divide all
the Java operators into the following groups:
• Arithmetic Operators
• Conditional or Relational Operators
• Logical Operators
• Shorthand or Assignment Operators
• Unary Operators
• Ternary Operators
• Bitwise Operators
Arithmetic Operators:
Arithmetic operators are used in mathematical expressions in the same way that
they are used in algebra.
Operator Description
Page 15
Core java
Example:
class Program
{
public static void main(String args[])
{
int a =10;
int b =20;
System.out.println("a + b = "+(a + b));
System.out.println("a - b = "+(a - b));
System.out.println("a * b = "+(a * b));
System.out.println("b / a = "+(b / a));
System.out.println("b % a = "+(b % a));
}
}
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
a + b = 30
a - b = -10
a * b = 200
b/a=2
b%a=0
E:\Core_Java>
Page 16
Core java
Operator Description
> Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of
right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
Example :
class Program
{
public static void main(String args[])
{
int a =10;
int b =20;
System.out.println("a == b = "+(a == b));
System.out.println("a != b = "+(a != b));
Page 17
Core java
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
E:\Core_Java>
Logical Operators:
Operator Description
&& Called Logical AND operator. If both the operands are non zero,
then the condition becomes true.
Page 18
Core java
Example :
class Program
{
public static void main(String args[])
{
boolean a =true;
boolean b =false;
System.out.println("a && b = "+(a&&b));
System.out.println("a || b = "+(a||b));
System.out.println("!(a && b) = "+!(a && b));
}
}
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
a && b = false
a || b = true
!(a && b) = true
E:\Core_Java>
Page 19
Core java
Assignment Operator :
Operator Description
Example :
class Program
{
public static void main(String args[])
{
int a =10;
int b =20;
int c =0;
Page 20
Core java
c = a + b;
System.out.println("c = a + b = "+ c );
c += a ;
System.out.println("c += a = "+ c );
c -= a ;
System.out.println("c -= a = "+ c );
c *= a ;
System.out.println("c *= a = "+ c );
a =10;
c =15;
c /= a ;
System.out.println("c /= a = "+ c );
a =10;
c =15;
c %= a ;
System.out.println("c %= a = "+ c );
}
}
Output :
E:\Core_Java>javac Program.java
E:\Core_Java>java Program
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
E:\Core_Java>
Page 21
Core java
Unary Operator :
1. Increment
i. Pre Increment ( ++ x)
ii. Post Increment ( x++)
2. Decrement
i. Pre Decrement ( --x)
ii. Post Decrement ( x--)
Pre Increment : It will increment the value first then assigns the value.
Post Increment : It will assign the value first then do the increment
operation
Pre Decrement : It will decrement the value first then assigns the value.
Post Decrement : It will assign the value first then do the Decrement
operation
Example
class IncDec
{
public static void main(String[] args)
{
int a=10,b=11;
int r1=a++ + a++ + --a + a++;
System.out.println("The result is "+r1);
System.out.println("The value of a is "+a);
System.out.println("The value of b is "+b);
}
}
Page 22
Core java
E:\PMKK\Core_Java>javac IncDec.java
E:\PMKK\Core_Java>java IncDec
The result is 43
The value of a is 12
The value of b is 11
E:\PMKK\Core_Java>
Page 23