SLM - Unit 02
SLM - Unit 02
2.1 Introduction
In the last unit, we have discussed various features of Java. In this unit, we
will discuss the basic programming structure in Java. Before that we all
know that 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 unit explains how to write object-oriented program using the Java
language syntax.
Objectives:
After studying this unit, you should be able to:
list the keywords significant to Java compiler
2.2 Keywords
Keywords are special words that are of significance to the Java compiler.
Currently JAVA language have 50 keywords (see Table 2.1). The Java
language is defined by these keywords combined with operators and
separators. Keywords cannot be used as variable, class, or method names.
The const and goto are reserved keywords but these are not used. In
addition to the above keywords Java also reserves the following: true, false,
and null.
The steps for compiling and running the program are shown in Figure 2.2.
The program is stored in a subdirectory called java. The above program
prints “Hello! How are you? “
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 bytecode 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. The platform for Java have Java Virtual Machine (JVM) and a
package of ready made software components. This package is known as
The byte, int, short, float, long 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
Based on primitive data types, the Abstract Datatypes have more
functionality in comparison primitive data types. An example of Abstract
datatypes is String, it can store digits, alphabets and special characters
such as /, (); :$#. Normal numerical operations cannot be performed on a
variable of String datatype even if the data stored in it has digits.
Self Assessment Questions
4. String is a ___________ data type in Java.
Output:
Compiling the source code....
$javac Box.java 2>&1
2.14 Summary
In this unit, you have learnt the following:
Creating Classes Using Java
The data members and methods of a class are defined inside the class
body. In Java, brackets {} 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.
candidateName
candidateAddress
candidateCourse
displayDetails()
2.16 Answers
Self Assessment Questions
1. bytecode
2. write once, run anywhere
public Candidate()
{
candidateName=”Arun”;
candidateAddress=”Manipal”;
candidateCourse=”BScIT”;
}