Sie sind auf Seite 1von 23

Core java

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

• Interpreted: Java byte code is translated on the fly to native machine


instructions and is not stored anywhere.
• High Performance: With the use of Just-In-Time compiles, java enables
high performance.
• Distributed: Java is designed for the distributed environment of the
internet.
• Dynamic: Java is considered to be more dynamic than C or C++ since it
is designed to adapt to an evolvimg environment.

Tools You Will Need


You will need a Pentium 200-MHz computer with a minimum of 64 MB
of RAM (128 MB of RAM recommended).
You will also need the following softwares:
• Linux 7.1 or Windows xp/7/8 operating system
• Java JDK 8
• Microsoft Notepad or any other text editor

How to Download Java


• Download Java 1.8
• Java SE Development Kit-8- download-oracle
• Accept the license
• Windows x86 for 32bit / Windows x64 for 64bit

Path setting of Java(Setting Environment Variable)


Assuming you have installed Java in C:\Program Files\java\jdk directory:
• Right click on "My Computer" and select " Properties".
• Click the "Environment variables" button under the "Advanced" tab.

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

Structure of the Java Program

class class_name
{
public static void main(String[ ] args)
{
//statement or code to execute
}
}

Page 3
Core java

First Java Program:


Let us look at a simple code that would print the words Hello World.
class MyFirstJavaProgram
{
/* This is my first java program. * This will print 'Hello World' as the output */
public static void main(String[ ]args)
{
System.out.println("Hello World");
// prints Hello World
}
}

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

To change the drive:


c:\users\user_name>D:
/* to change the path from c to d drive*/
D:\cd core_java
/* core_java is the folder in the drive where java program saved */
D:\core_java>
Basic Syntax:
About Java programs, it is very important to keep in mind the
following points.
Case Sensitivity - Java is case sensitive, which means identifier Hello
and hello would have different meaning in Java.
Class Names - For all class names, the first letter should be in Upper
Case. If several words are used to form a name of the class, each inner
word's first letter should be in Upper Case.
Example class MyFirstJavaClass
Method Names - All method names should start with a Lower Case
letter. If several words are used to form the name of the method, then
each inner word's first letter should be in Upper Case.
Example public void myMethodName()
Program File Name - Name of the program file should exactly match the
class name. When saving the file, you should save it using the class name
(Remember Java is case sensitive) and append '.java' to the end of the
name (if the file name and the class name do not match your program will
not compile).
Example : Assume 'MyFirstJavaProgram' is the class name, then
the file should be saved as'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing starts
from the main() method, which is a mandatory part of every Java
program.

Page 5
Core java

Developing Java Program Includes 3 Steps


1. Source Code
2. Compilation
3. Execution

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

provided JRE`s installed on the system. This concept is known as platform


independent.
We cannot run the .class file without JRE, Hence it is dependent on JRE.
While compiling if we get any error it is known as "Compile time
error", until we fix the compile time error the compiler will not generate the
.class file.
Any error occuring during the executable time, that is known as "run
time error", to fix the run time error go to the source code, and do the required
changes, recompile it and then execute it.
JDK is a development kit containing the java compiler and JRE and also
few development tools needed for developing the java applications.
We use text editor or integrated development environment (IDE) tools to
create the source code.

Example : 1. Text Editor:- Notepad, Notepad++,editplus


2. IDE :- Eclipse, Netbeans

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>

Difference between print() and println()

• 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

Final Keyword in Java


If we declare variable as a final, we cant able to reinitialize value ....
Example :
class Program
{
public static void main(String[] args)
{
final int pin=1234;
pin=4321;
System.out.println(pin);
}
}
Output :
E:\Core_Java>javac Program.java
Program.java:6: error: cannot assign a value to final variable pin
pin=4321;
^
1 error

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

+ Addition - Adds values on either side of the operator

- Subtraction - Subtracts right hand operand from left hand operand

* Multiplication - Multiplies values on either side of the operator

/ Division - Divides left hand operand by right hand operand

% Modulus - Divides left hand operand by right hand operand and


returns remainder

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

Conditional or Relational Operators

Operator Description

== Checks if the values of two operands are equal or not, if yes


then condition becomes true.

!= Checks if the values of two operands are equal or not, if values


are not equal then condition becomes true.

> 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 greater than or equal to


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

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 = 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.

|| Called Logical OR Operator. If any of the two operands are non-


zero, then the condition becomes true.

! ! Called Logical NOT Operator. Use to reverses the

logical state of its operand.

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

= Simple assignment operator, Assigns values from right side


operands to left side operand

+= Add AND assignment operator, It adds right operand to the


left operand and assign the result to left operand

-= Subtract AND assignment operator, It subtracts right operand


from the left operand and assign the result to left operand

*= Multiply AND assignment operator, It multiplies right


operand with the left operand and assign the result to left
operand

/= Divide AND assignment operator, It divides left operand


with the right operand and assign the result to left operand

%= Modulus AND assignment operator, It takes modulus using


two operands and assign the result to left operand

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

Das könnte Ihnen auch gefallen