You are on page 1of 28

JAVA BASICS

Confidential
What is Java?
• Java is a high-level, third generation
programming language, like C, Fortran and
can be used to write computer applications
that crunch numbers, process words, play
games, store data or do any of the thousands
of other things computer software can do.
• Java technology is both a programming
language and a platform.

Confidential
JAVA PLATFORM
• A platform is the hardware or software
environment in which a program runs.
• The Java platform has two components:
The Java Virtual Machine
The Java Application Programming Interface (API)

Confidential
JAVA BUZZ WORDS
• OBJECT ORIENTED
• PLATFORM INDEPENDENT
• SIMPLE
• SECURE
• SUITABLE FOR WEB APPLICATION
• ROBUST
• PORTABLE
• EXCEPTION HANDLING
• MULTI THREADING
• BUILT IN API
• OPEN SOURCE

Confidential
OBJECT ORIENTED PROGRAMMING
• In object-oriented programs data is represented by
objects. Objects have two sections
• Fields (Instance variables) tell you what an
object is.
• Methods tell you what an object does.
These fields and methods are closely tied to the
object's real world characteristics and behavior.

Confidential
OOP Principles
• ENCAPSULATION
– Binding of attributes and behaviour inside a class
• INHERITANCE
– Deriving the properties of one class by the other
• POLYMORPHISM
– Using the same name for different types of actions

Confidential
PLATFORM INDEPENDENT
• Java is compiled to an intermediate form
called byte-code, a special native program
called the Java interpreter reads the byte code
and executes the corresponding native
machine instructions.

Confidential
COMPILATION
SOURCE CODE COMPILER BYTE CODE

.JAVA FILE JAVAC.EXE .CLASS FILE

EXECUTION
BYTE CODE INTERPRETER MACINE CODE

.CLASS FILE JAVA.EXE .EXE FILE

Confidential
Portability
Three features make Java programs portable:

1. The language. The Java language is completely


specified; all data-type sizes and formats are defined
as part of the language.

2. The library. The Java class library is available with a


Java runtime system, because a portable program is
of no use if you cannot use the same class library on
every platform.

Confidential
Portability
3. The byte code. The Java runtime system does
not compile your source codeirectly into
machine language Instead, Java programs are
translated into machine-independent byte
code. The byte code is easily interpreted and
therefore can be executed on any platform
having a Java runtime system.

Confidential
Security
There are two main lines of defense:
• Interpreter level:
• No pointer arithmetic
• Garbage collection
• Array bounds checking
• No illegal data conversions
• Browser level (applies to applets only):
• No local file I/O
• Sockets back to host only
• No calls to native methods

Confidential
Robustness
• The Java language is robust. It has several
features designed to avoid crashes
• during program execution, including:
• No pointer arithmetic
• Garbage collection--no bad addresses
• Array and string bounds checking
• No jumping to bad method addresses
• Interfaces and exceptions

Confidential
EXCEPTIONS HANDLING
• Java is having a special type of exception handling
mechanism which allows the execution of the
program even after getting a runtime bug.
• It is handled by using a set of classes & keywords like
• try
• catch
• finally
• throw
• throws

Confidential
JAVA IS MULTITHREADED
• A single Java program can have many different
threads executing independently and
continuously.
• This makes Java very responsive to user input.
• For this it uses a built in features like
• Thread class
• Runnable interface

Confidential
BUILT IN API
• Java has a rich set of collection of built in APIs,
which facilitates the programmer with much
of the programming aspects. Like,
– java.lang
– java.io
– java.util
– java.net and so on.,

Confidential
Coding Hello World Application
class HelloWorld
{
public static void main (String args[]) {
System.out.println("Hello World!"); }
}

Confidential
SAVING
• We can save a java application by
– Taking the class name which has the main()
– Taking the class or interface declared as public
Tip:
enclose the filename in double quotes as Notepad
add a three letter ".txt" extension to all the files
they save without telling you. Thus you can
unexpectedly end up with a file called
"HelloWorld.java.txt."

Confidential
Compilation & Execution
Under Windows, it's similar. You just do this in a DOS
shell.
C:> javac HelloWorld.java
C:> java HelloWorld
Hello World
C:>
Notice that you use the .java extension when
compiling a file, but you do not use the .class
extension when running a file.

Confidential
JAVA BUILDING BLOCKS

Confidential
JAVA TOKENS
• WHITE SPACE
• COMMENTS
• IDENTIFIERS
• LITERALS
• SEPERATORS
• OPERATORS
• DATA TYPES
• CONTROL STATEMENT
• ARRAYS
• CLASSES, OBJECTS, METHODS
• KEYWORDS

Confidential
WHITE SPACE
• Blank space introduced for proper functioning
of a program
• Java is a free form language which never cares
about white spaces

Confidential
COMMENTS
• It’s a non executable statement provided to
get a brief idea about the modules of
applications
• Types:
– Single line://……comment……
– Multi line:/*......comment…..*/
– Documentation:/**……comment……*/

Confidential
IDENTIFIERS
• Group of uppercase, lowercase characters,
numbers, and some special characters
• Rules:
– No special characters other than _ and $
– It should not start with numbers
– No space in between

Confidential
LITERALS
We can create a literal of a data type by creating
the constant representation of it.
• Char  ‘a’
• Numbers  100
• Float  10.09f
• Double  10.09
• Boolean  true, false

Confidential
SEPERATORS
• {} Braces
• () Parenthesis
• [] Brackets
• ; Semi colon
• , Comma
• . Access operator

Confidential
OPERATORS
• Arithmetic
• Bitwise
• Logical
• Relational

Confidential
DATA TYPES
TYPE KEYWORD SIZE
CHARACTER char 16

NUMBERS byte 8
short 16
int 32
long 64
FLOATING Pt float 32
double 64
BOOLEAN boolean 1
Confidential
CONTROL STATEMENTS
• Selection
– if, switch
• Iteration
– while, do-while, for
• Jump
– break, continue, return

Confidential

You might also like