Unit I-Basic Syntactical Constructs in Java
Unit I-Basic Syntactical Constructs in Java
JAVA
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 name
from Oak to Java
Java Example
Let's have a quick look at Java programming example. A detailed description of Hello Java
example is available in next page.
Simple.java
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Test it Now
Application
According to Sun, 3 billion devices run Java. There are many devices where Java is currently
used. Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
1
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
History of Java
1) James Gosling
, Mike Sheridan, and Patrick Naughton initiated the Java language project in June
1991. The small team of sun engineers called Green Team.
2) Initially it was designed for small, embedded systems
in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
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 the most important features of the Java language is given below.
1. Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun Microsystem, Java language is a simple programming language
because:
o Java syntax is based on C++ (so easier for programmers to learn it after C++).
2
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
o Java has removed many complicated and rarely-used features, for example,
explicit pointers, operator overloading, etc.
o There is no need to remove unreferenced objects because there is an Automatic
Garbage Collection in Java.
2. Object-oriented
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different types of
objects that incorporate both data and behaviour. Object-oriented programming (OOPs)
is a methodology that simplifies software development and maintenance by providing
some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
3. Platform Independent
3
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
4. Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is
secured because:
o No explicit pointer
o Java Programs run inside a virtual machine sandbox
8. High-performance
Java is faster than other traditional interpreted programming languages because Java
bytecode is "close" to native code. It is still a little bit slower than a compiled language
(e.g., C++). Java is an interpreted language that is why it is slower than compiled
languages, e.g., C, C++, etc.
4
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
9. Distributed
Java is distributed because it facilitates users to create distributed applications in Java.
RMI and EJB are used for creating distributed applications. This feature of Java makes
us able to access files by calling the methods from any machine on the internet.
10. Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java programs
that deal with many tasks at once by defining multiple threads. The main advantage of
multi-threading is that it doesn't occupy memory for each thread. It shares a common
memory area. Threads are important for multi-media, Web applications, etc.
11. Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes
are loaded on demand. It also supports functions from its native languages, i.e., C and
C++. Java supports dynamic compilation and automatic memory management (garbage
collection).
C++ vs Java
There are many differences and similarities between the C++ programming language
and Java. A list of top differences between C++ and Java are given below:
Comparis C++ Java
on Index
Platform- C++ is platform- Java is platform-independent.
independe dependent.
nt
Mainly C++ is mainly used for Java is mainly used for application
used for system programming. programming. It is widely used in
Windows-based, web-based,
enterprise, and mobile
applications.
Design C++ was designed for Java was designed and created as
Goal systems and applications an interpreter for printing systems
programming. It was an but later extended as a support
extension of the C network computing. It was
programming language. designed to be easy to use and
accessible to a broader audience.
Goto C++ supports Java doesn't support the goto
the goto statement. statement.
Multiple C++ supports multiple Java doesn't support multiple
inheritanc inheritance. inheritance through class. It can be
e achieved by using interfaces in
java.
Operator C++ supports operator Java doesn't support operator
Overloadi overloading. overloading.
ng
Pointers C++ supports pointers. Java supports pointer internally.
You can write a pointer However, you can't write the
program in C++. pointer program in java. It means
java has restricted pointer support
in java.
Compiler C++ uses compiler only. Java uses both compiler and
and C++ is compiled and run interpreter. Java source code is
Interprete using the compiler converted into bytecode at
r which converts source compilation time. The interpreter
code into machine code executes this bytecode at runtime
5
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
so, C++ is platform and produces output. Java is
dependent. interpreted that is why it is
platform-independent.
Call by C++ supports both call Java supports call by value only.
Value and by value and call by There is no call by reference in
Call by reference. java.
reference
Structure C++ supports structures Java doesn't support structures and
and Union and unions. unions.
Thread C++ doesn't have built- Java has built-in thread support.
Support in support for threads. It
relies on third-party
libraries for thread
support.
Document C++ doesn't support Java supports documentation
ation documentation comment (/** ... */) to create
comment comments. documentation for java source
code.
Virtual C++ supports virtual Java has no virtual keyword. We
Keyword keyword so that we can can override all non-static methods
decide whether or not to by default. In other words, non-
override a function. static methods are virtual by
default.
unsigned C++ doesn't support Java supports unsigned right shift
right shift >>> operator. >>> operator that fills zero at the
>>> top for the negative numbers. For
positive numbers, it works same
like >> operator.
Inheritanc C++ always creates a Java always uses a single
e Tree new inheritance tree. inheritance tree because all classes
are the child of the Object class in
Java. The Object class is the root of
the inheritance tree in java.
Hardware C++ is nearer to Java is not so interactive with
hardware. hardware.
Object- C++ is an object- Java is also an object-
oriented oriented language. oriented language. However,
However, in the C everything (except fundamental
language, a single root types) is an object in Java. It is a
hierarchy is not possible. single root hierarchy as everything
gets derived from java.lang.Object.
Note
o Java doesn't support default arguments like C++.
o Java does not support header files like C++. Java uses the import keyword to include
different classes and methods.
Java Variables
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location.
There are three types of variables in java: local, instance and static.
Variable
6
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
A variable is the name of a reserved area allocated in memory. In other words, it is a
name of the memory location. It is a combination of "vary + able" which means its
value can be changed.
5
int data=50;//Here data is variable
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
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 an instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not
shared among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be
local. You can create a single copy of the static variable and share it among all
the instances of the class. Memory allocation for static variables happens only
once when the class is loaded in the memory.
7
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
Example to understand the types of variables in java
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
Java Variable Example: Add Two Numbers
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Output:
20
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.
36.3M
776
9
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
The Boolean data type is used to store only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be
defined precisely.
Example:
Boolean one = false
Byte Data Type
The byte data type is an example of primitive data type. It isan 8-bit signed
two's complement integer. Its value-range lies between -128 to 127 (inclusive).
Its minimum value is -128 and maximum value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory
savings is most required. It saves space because a byte is 4 times smaller than
an integer. It can also be used in place of "int" data type.
Example:
byte a = 10, byte b = -20
Example:
short s = 10000, short r = -5000
Example:
int a = 100000, int b = -200000
10
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
9,223,372,036,854,775,807. Its default value is 0. The long data type is used
when you need a range of values more than those provided by int.
Example:
long a = 100000L, long b = -200000L
Example:
float f1 = 234.5f
Example:
double d1 = 12.3
Example:
char letterA = 'A'
11