Unit 2 Basics of Java: Structure
Unit 2 Basics of Java: Structure
Unit 2 Basics of Java: Structure
Unit 2
Unit 2
Basics of Java
Structure
2.1
Introduction
Objective
2.2
Keywords
2.3
2.4
2.5
2.5.2
2.6
Variables in Java
2.6.1
Naming Variables
Self Assessment Questions
2.7
2.8
2.9
2.10
2.11
Invoke methods
Self Assessment Questions
2.12
2.13
2.14
2.15
Summary
2.16
Terminal Questions
Page No. 13
Java Programming
Unit 2
2.1 Introduction
The English language has a vocabulary a set of words that have certain
meaning. It also provides us with rules for using the vocabulary English
grammar. The Java language also provides a vocabulary and a set of rules
to use the vocabulary. The vocabulary is represented through a set of
keywords and the grammar is the syntax of the language.
This lesson explains how to write object-oriented program using the Java
language syntax.
Objectives
In this chapter, you will learn about the:
Java Keyword
Data types in Java
Variable naming conventions
Initializing Variables.
Literals
2.2 Keywords
Keywords are special words that are of significance to the Java compiler.
You cannot use keywords to name classes or variables. The table below
contains a list of Java keywords.
Abstract
boolean
break
byte
case
Catch
Char
Class
const
continue
Default
Do
Double
else
extends
Final
Finally
Float
for
Goto
If
Implements
Import
instanceof
Int
interface
Long
Native
new
Package
Private
protected
Public
return
Short
Static
Super
Switch
synchronized
This
Throw
throws
Transient
try
Void
Volatile
While
Page No. 14
Java Programming
Unit 2
Figure 2.1
The step of compiling and running the program is shown in figure 2.2. The
program is stored in a subdirectory called java. The above program when
run prints the message Hello! How are you?
Page No. 15
Java Programming
Unit 2
Figure 2.2
The concept of write once, run anywhere is possible in Java. The Java
program can be compiled on any platform having a Java compiler. The
resulting bytecodes can then be run on Window NT or Solaris or Macintosh
or any other machine. The machine should have a Java platform to run Java
code. Java platform consists of Java Virtual Machine (JVM) and a package
of ready made software components. This package is known as The Java
Application Programming Interface (Java AP!). The compiled Java program
can run on any hardware platform having Java Virtual Machine (JVM)
installed on it.
Self Assessment Questions
1. A compiler converts the Java program into an intermediate language
representation called ____________.
2. The concept of _________________ is possible in Java.
Page No. 16
Java Programming
Unit 2
/* Comments go here
More comments here
*/
Type 2
// this information is ignored by the compiler till the end of the line.
Type 3
/* documentation comment */
In the first type comments can spread over multiple lines. In the second type
the information written after // is ignored by the compiler. The third type of
comment is used by a tool called javadoc for automatic generation of
documentation.
Self Assessment Questions
1. The third type of comment is used by a tool called ___________ for
automatic generation of documentation.
Page No. 17
Java Programming
Data Type
byte
Unit 2
Size/Form
at (bits)
8
Description
Range
Byte-length integer
short
16
Short integer
-2
15
to 2
15
-1
int
32
integer
-2
31
to 2
31
-1
63
to 2
63
-1
long
64
Long integer
-2
float
32
+/- about 10
39
double
64
+/- about 10
317
char
16
A single character
Boolean
The data types byte, short, int, long, float and double are numeric data
types. The first four of these can hold only whole numbers whereas the last
two (float and double) can hold decimal values like 5.05. All these data
types can hold negative values. However, the keyword unsigned can be
used to restrict the range of values to positive numbers. Amongst others,
boolean can hold only the value true or false and char can hold only a single
character.
2.5.2 Abstract/Derived Data Types
Abstract data types are based on primitives data types and have more
functionality that the primitive data types. For example, String is an abstract
data type that can store alphabets, digits and other characters like /, (); :$#.
You cannot perform calculations on a variable of the string data type even if
the data stored in it has digits.
Self Assessment Questions
1. What are the different type of data types in java ?
Sikkim Manipal University
Page No. 18
Java Programming
Unit 2
Page No. 19
Java Programming
Unit 2
Page No. 20
Java Programming
Unit 2
Page No. 21
Java Programming
Unit 2
System is one of the most important and useful classes provided by Java. It
provides a standard interface to common system resources like the display
device and the keyboard.
The out Object
It is an object encapsulated inside the System class, and represents the
standard output device. This object contains the println () method.
The println () method
The println () method displays the data on the screen.
Example
System.out.println (Hello World);
Will display Hello World on the screen.
Page No. 22
Java Programming
Unit 2
Page No. 23
Java Programming
Unit 2
Page No. 24
Java Programming
Unit 2
2.15 Summary
Creating Classes Using Java
The data members and methods of a class are defined inside the class
body. In java, braces {} mark the beginning and end of a class or method.
The class keyword is used to declare a class.
Coding Methods of a class
Methods provide functionality to classes. In Java, methods are declared
in the class body.
Declaring Objects
The new operator is used to create a class object.
Displaying Data on Screen
The System class is one of the most important and useful classes
provided by java. It provides a standard interface to common system
resources like the display device and the keyboard.
The println () method displays the data on the screen.
Compiling a Java Program
In Java, the program is compiled into bytecode (.class file) that runs on
the Java Virtual Machine, which can interpret and run the program on
any operating system. This makes Java programs platform-independent.
Page No. 25
Java Programming
Unit 2
Page No. 26