Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views32 pages

D-1(Concept_of_JAVA_programming)

Summary of Java Programming Concepts Java is a robust, object-oriented, and secure high-level programming language and platform developed by Sun Microsystems in 1995. James Gosling is known as the father of Java. Initially, it was called Oak, but the name was changed because Oak was already a registered company.

Uploaded by

cocomealy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

D-1(Concept_of_JAVA_programming)

Summary of Java Programming Concepts Java is a robust, object-oriented, and secure high-level programming language and platform developed by Sun Microsystems in 1995. James Gosling is known as the father of Java. Initially, it was called Oak, but the name was changed because Oak was already a registered company.

Uploaded by

cocomealy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Concept of JAVA programming

platform and its important properties

Day 9
What is Java

Java is a programming language and a platform. Java is a high level, robust, object-
oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in
the year 1995. James Gosling is known as the father of Java. Before Java, its name
was Oak. Since Oak was already a registered company, so James Gosling and his team
changed the Oak name to Java.
Platform: Any hardware or software environment in which a program runs, is known
as a platform. Since Java has a runtime environment (JRE) and API, it is called a
platform.
Java Platforms / Editions

1. Java SE (Java Standard Edition) : It is a Java programming platform. It includes


Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math
etc. It includes core topics like OOPs, String, Regex, Exception, Inner classes,
Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection, etc.
2. Java EE (Java Enterprise Edition) : It is an enterprise platform which is mainly
used to develop web and enterprise applications. It is built on the top of the Java SE
platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA, etc.
3. Java ME (Java Micro Edition) : It is a micro platform which is mainly used to
develop mobile applications.
Features of Java
The primary objective of Java programming language creation was to make it portable, simple and secure
programming language. Apart from this, there are also some excellent features which play an important role
in the popularity of this language. The features of Java are also known as java buzzwords.
A list of most important features of Java language is given below.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Comparison Index C++ Java
Platform-independent C++ is platform-dependent. Java is platform-independent.
Mainly used for C++ is mainly used for system Java is mainly used for application programming. It is
programming. widely used in window, web-based, enterprise and
mobile applications.
Design Goal C++ was designed for systems and Java was designed and created as an interpreter for
applications programming. It was an printing systems but later extended as a support
extension of C programming network computing. It was designed with a goal of
language. being easy to use and accessible to a broader audience.
Goto C++ supports goto statement Java doesn't support the goto statement.
Multiple inheritance C++ supports multiple inheritance. Java doesn't support multiple inheritance through class.
It can be achieved by interfaces in java.
Operator Overloading C++ supports operator overloading. Java doesn't support operator overloading.
Pointers C++ supports pointers. You can Java supports pointer internally. However, you can't
write pointer program in C++. write the pointer program in java. It means java has
restricted pointer support in java.
Compiler and C++ uses compiler only. C++ is compiled Java uses compiler and interpreter both. Java source
Interpreter and run using the compiler which converts code is converted into bytecode at compilation time.
source code into machine code so, C++ is The interpreter executes this bytecode at runtime and
platform dependent. produces output. Java is interpreted that is why it is
platform independent.
Call by Value and Call C++ supports both call by value and call Java supports call by value only. There is no call by
by reference by reference. reference in java.
Structure and Union C++ supports structures and unions. Java doesn't support structures and unions.
Virtual Keyword C++ supports virtual keyword so that we Java has no virtual keyword. We can override all non-
can decide whether or not override a static methods by default. In other words, non-static
function. methods are virtual by default.
Inheritance Tree C++ creates a new inheritance tree always. Java uses a single inheritance tree always because all
classes are the child of Object class in java. The object
class is the root of the inheritance tree in java.
Hardware C++ is nearer to hardware. Java is not so interactive with hardware.
Object-oriented C++ is an object-oriented language. Java is also an object-oriented language. However,
However, in C language, single root everything (except fundamental types) is an object in
hierarchy is not possible. Java. It is a single root hierarchy as everything gets
derived from java.lang.Object.
Java Example
Let's have a quick look at Java programming example. A detailed description of Hello Java
example is available in next page.
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}

save this file as Sample.java

Output:
Hello Java
Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().

• Class keyword is used to declare a class in java.


• Public keyword is an access modifier which represents visibility. It means it is visible to all.
• Static is a keyword. If we declare any method as static, it is known as the static method. The core
advantage of the static method is that there is no need to create an object to invoke the static method.
The main method is executed by the JVM, so it doesn't require to create an object to invoke the main
method. So it saves memory.
• Void is the return type of the method. It means it doesn't return any value.
• Main represents the starting point of the program.
• String[] args is used for command line argument.
• System.out.println() is used to print statement. Here, System is a class, out is the object of
PrintStream class, println() is the method of PrintStream class. We will learn about the internal working
of System.out.println statement later.
JVM

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't
physically exist. It is a specification that provides a runtime environment in which Java bytecode can be
executed. It can also run those programs which are written in other languages and compiled to Java
bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform
dependent because the configuration of each OS is different from each other. However, Java is
platform independent. There are three notions of the JVM: specification, implementation, and instance.
The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime
Environment is a set of software tools which are used for developing Java applications. It is used to
provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of
libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun Micro Systems.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applications and applets. It physically exists. It
contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
• Standard Edition Java Platform
• Enterprise Edition Java Platform
• Micro Edition Java Platform

The JDK contains a private Java Virtual


Machine (JVM) and a few other resources
such as an interpreter/loader (java),
a compiler (javac), an archiver (jar),
a documentation generator (Javadoc), etc.
to complete the development of a Java
Application.
Types of Variables

There are three types of variables in Java:


1. Local Variable : A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even aware that the
variable exists.
A local variable cannot be defined with "static" keyword.

2. Instance Variable : A variable declared inside the class but outside the body of the method, is called
instance variable. It is not declared as static.
It is called instance variable because its value is instance specific and is not shared among instances.

3. Static variable :
A variable which is declared as static is called static variable. It cannot be local. You can create a single
copy of static variable and share among all the instances of the class. Memory allocation for static
variable happens only once when the class is loaded in the memory.
Example to understand the types of variables in java
class A
{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Java Variable Example: Widening
class Simple
{
public static void main(String[] args)
{
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}
}

Output:
10
10.0
Java Variable Example: Narrowing (Typecasting)
class Simple
{
public static void main(String[] args)
{
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}
}

Output:
10.5
10
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There are two types of
data types in Java:
1. Primitive Data Types : The primitive data types include boolean, char, byte, short, int, long, float
and double.
2. Non-primitive Data Types : The non-primitive data types include Classes, Interfaces, and Arrays.

Java Primitive Data Types


In Java language, primitive data types are the building blocks of data manipulation. These are the most
basic data types available in Java language.
Java is a statically-typed programming language. It means, all variables must be declared before its use.
That is why we need to declare variable's type and name.
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Operators in Java
Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Java Unary Operator Example: ++ and --

class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}
}

Output:
10
12
12
10
Java Unary Operator Example 2: ++ and --
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}
}

Output:
22
21
Java Unary Operator Example: ~ and !
class OperatorExample
{ public static void main(String args[])
{
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//11 (minus of total positive value which starts from 0)
System.out.println(~b); //9 (positive of total minus, positive starts from 0)
System.out.println(!c); //false (opposite of boolean value)
System.out.println(!d); //true
}
} Output:
-11
9
false
true
Java Left Shift Operator Example

class OperatorExample
{
public static void main(String args[])
{
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
}
}

Output:
40
80
2
5
Java AND Operator Example : Logical && and Bitwise &
The logical && operator doesn't check second condition if first condition is false. It
checks second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or
false.
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}
}

Output:
false
false
Java OR Operator Example : Logical || and Bitwise |
The logical || operator doesn't check second condition if first condition is true. It checks
second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
class OperatorExample
{ public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true Output:
System.out.println(a>b|a<c);//true | true = true true
//|| vs | true
System.out.println(a>b||a++<c);//true || true = true true
System.out.println(a);//10 because second condition is not checked 10
System.out.println(a>b|a++<c);//true | true = true true
System.out.println(a);//11 because second condition is checked 11
}
}
Object and Class Example: main within the class
In this example, we have created a Student class which has two data members id and
name. We are creating the object of the Student class by new keyword and printing the
object's value.
File: Student.java
class Student
{ int id;
String name;
public static void main(String args[])
{
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

Output:
0
null
Object and Class Example: Object Initialization through reference
Initializing an object means storing data into the object. Let's see a simple example where
we are going to initialize the object through a reference variable.
File: TestStudent.java
class Student
{ int id; String name; }
class TestStudent
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.id=10;
Output:
s1.name=“Pintu";
10 Pintu
s2.id=11;
11 Aman
s2.name="Aman";
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case, Java compiler
provides a default constructor by default.
There are two types of constructors in Java: no-arg constructor, and parameterized constructor.

Types of Java constructors


There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Example of default constructor
class Car
{
Car()
{
System.out.println(“Car is beautiful");
}
public static void main(String args[])
{
Car b=new Car(); //calling a default constructor
}
}

Output:
Car is beautiful
Example of default constructor
class Student3
{ int id; String name;
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student3 s1=new Student3(); Output:
Student3 s2=new Student3(); 0 null
0 null
s1.display();
Explanation:
s2.display(); In the above class, you are not creating any
} constructor so compiler provides you a default
} constructor. Here 0 and null values are
provided by default constructor.
THANK YOU!

You might also like