Professional Documents
Culture Documents
02 Unit2
02 Unit2
02 Unit2
Unit 2
Unit 2
Structure 2.1 2.2 2.3 2.4 2.5 Introduction Objective Keywords How does it work? Self Assessment Questions How to include comments Self Assessment Questions Data Types in Java 2.5.1 2.5.2 2.6 Primitives Data Types Abstract/Derived Data Types Self Assessment Questions Variables in Java 2.6.1 2.7 Naming Variables Self Assessment Questions Using classes in java 2.7.1 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 Standard for coding Self Assessment Questions Declaring methods in java Self Assessment Questions Code to display Test Value The main method Invoke methods Self Assessment Questions How to Save a Java Program Compiling Java Programs Executing a Java Program Summary Terminal Questions
Basics of Java
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 Catch Default Final If interface Private Static Throw Volatile boolean Char Do Finally Implements Long protected Super throws While Page No. 14 break Class Double Float Import Native Public Switch Transient byte const else for instanceof new return synchronized try case continue extends Goto Int Package Short This Void
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.
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 automatic generation of documentation. for
2.5.1 Primitives Data Types Primitive data types (also know as standard data types) are the data types that are built into the Java language. The Java compiler holds details instructions on each operation the data types that are built into the Java language. The Java compiler holds detailed instructions on each legal operation the data type supports. There are eight primitive data types in Java.
Page No. 17
Java Programming
Unit 2
Size/Form at (bits) 8
16 32 64 32 64 16 1
Short integer integer Long integer Single precision floating point Double precision floating point A single character A boolean value (true or false)
317
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
Syntax for Defining Variables All the attributes of a class are defined as data members. The syntax used to declare a class variable is: <data_type> <variable_name> As the braces { } are used to mark the beginning and end of a class, a semicolon ; is used to mark the end of a statement. Self Assessment Questions 1. What are the rules for naming variable in Java?
Java Programming
Unit 2
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
Java Programming
Unit 2
Employee emp =new Employee (); emp.display (); } } Self Assessment Questions 1. operator is used to create an object.
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
Executing a Java program When the code is compiled and error-free, the program can be executed by issuing the following command: java <filename>
Page No. 26