0% found this document useful (0 votes)
81 views11 pages

Unit I-Basic Syntactical Constructs in Java

This document provides an overview of the Java programming language. It discusses that Java was developed by Sun Microsystems in 1995 and is an object-oriented, robust, and secure language. It also summarizes the different types of Java applications that can be created including standalone, web, enterprise, and mobile applications. Additionally, it outlines some of the key features of Java such as being simple, object-oriented, platform independent, secured, robust, architecture-neutral, portable, high-performance, distributed, multi-threaded, and dynamic.

Uploaded by

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

Unit I-Basic Syntactical Constructs in Java

This document provides an overview of the Java programming language. It discusses that Java was developed by Sun Microsystems in 1995 and is an object-oriented, robust, and secure language. It also summarizes the different types of Java applications that can be created including standalone, web, enterprise, and mobile applications. Additionally, it outlines some of the key features of Java such as being simple, object-oriented, platform independent, secured, robust, architecture-neutral, portable, high-performance, distributed, multi-threaded, and dynamic.

Uploaded by

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

UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF

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.

Types of Java Applications


There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications. These are traditional software that we need to install on every machine.
Examples of standalone application are Media player, antivirus, etc. AWT and Swing are
used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a web
application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are
used for creating web applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called an
enterprise application. It has advantages like high-level security, load balancing, and
clustering. In Java, EJB is used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application. Currently,
Android and Java ME are used for creating mobile applications.

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

Java is platform independent because it is different from other languages like C, C++,


etc. which are compiled into platform specific machines while Java is a write once,
run anywhere language. A platform is the hardware or software environment in
which a program runs.
There are two types of platforms software-based and hardware-based. Java provides
a software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-
based platform that runs on top of other hardware-based platforms. It has two
components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun
Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into
bytecode. This bytecode is a platform-independent code because it can be run on
multiple platforms, i.e., Write Once and Run Anywhere (WORA).

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

o Classloader: Classloader in Java is a part of the Java Runtime Environment


(JRE) which is used to load Java classes into the Java Virtual Machine
dynamically. It adds security by separating the package for the classes of the local
file system from those that are imported from network sources.
o Bytecode Verifier: It checks the code fragments for illegal code that can
violate access rights to objects.
o Security Manager: It determines what resources a class can access such as
reading and writing to the local disk.
Java language provides these securities by default. Some security can also be provided
by an application developer explicitly through SSL, JAAS, Cryptography, etc.
5. Robust
The English mining of Robust is strong. Java is robust because:
o It uses strong memory management.
o There is a lack of pointers that avoids security problems.
o Java provides automatic garbage collection which runs on the Java Virtual
Machine to get rid of objects which are not being used by a Java application
anymore.
o There are exception handling and the type checking mechanism in Java. All these
points make Java robust.
6. Architecture-neutral
Java is architecture neutral because there are no implementation dependent features,
for example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and
4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for
both 32 and 64-bit architectures in Java.
7. Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It
doesn't require any implementation.

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

Java Variable Example: Widening


public class Simple{  
public static void main(String[] args){  
int a=10;  
float f=a;  
System.out.println(a);  
System.out.println(f);  
}}  

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.
8
UNIT I-BASIC SYNTACTICAL CONSTRUCTS IN SYIF
JAVA
There are 8 types of primitive data types:

36.3M

776

Exception Handling in Java - Javatpoint

o boolean data type


o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type

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
Boolean Data Type

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  

Short Data Type


The short data type is a 16-bit signed two's complement integer. Its value-
range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768
and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A
short data type is 2 times smaller than an integer.

Example:
short s = 10000, short r = -5000  

Int Data Type


The int data type is a 32-bit signed two's complement integer. Its value-range
lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its
minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its
default value is 0.
The int data type is generally used as a default data type for integral values
unless if there is no problem about memory.

Example:
int a = 100000, int b = -200000  

Long Data Type


The long data type is a 64-bit two's complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is -
9,223,372,036,854,775,808and maximum value is

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  

Float Data Type


The float data type is a single-precision 32-bit IEEE 754 floating point.Its
value range is unlimited. It is recommended to use a float (instead of double)
if you need to save memory in large arrays of floating point numbers. The float
data type should never be used for precise values, such as currency. Its default
value is 0.0F.

Example:
float f1 = 234.5f  

Double Data Type


The double data type is a double-precision 64-bit IEEE 754 floating point. Its
value range is unlimited. The double data type is generally used for decimal
values just like float. The double data type also should never be used for
precise values, such as currency. Its default value is 0.0d.

Example:
double d1 = 12.3  

Char Data Type


The char data type is a single 16-bit Unicode character. Its value-range lies
between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is
used to store characters.

Example:
char letterA = 'A'  

11

You might also like