You are on page 1of 10

Advanced Java (CSE-404-E)

Unit-1: Core Java


1.1 Introduction to JAVA
JAVA IS AN OBJECT-ORIENTED PROGRAMMING LANGUAGE THAT WAS DESIGNED TO MEET THE NEED FOR A PLATFORM INDEPENDENT LANGUAGE. Java is used to create applications that can run on a single computer as well as distributed network. Java is both a language and a technology used to develop stand-alone and Internet-based applications. Java technology is simultaneously a programming language and a platform.

1.

Java as a Programming Language

Java is a general-purpose high-level object programming language that provides the following characteristics: Simple Object Oriented Interpreted Architecture-Neutral Platform Independent Multithread Dynamic High-Performance Distributes Robust Secure

Usually, a programming language requires either a compiler or an interpreter to translate a code written in high-level programming language (source code) into its machine language (object code) equivalent. But, to run a Java Program, Java uses a combination of compiler and interpreter. The compiler translates the Java program into an intermediate code called Java byte code. Java interpreter is used to run the compiled Java Byte Code. This allows Java Programs to be executed on different platforms.
Java source code (Sample.java ) Compile r Java byte code (Sample.class) Interprete r Object Code

Java as a compiler and interpreter

Advanced Java (CSE-404-E)


Java is a strongly typed language, means, every variable (or expression) has a type associated with it and the type is known at the time of compilation. Thus, in Java, data type conversions do not take place implicitly. There are three kinds of Java Programs: Applications. These are stand-alone programs that run on a computer. Applets. These are programs that run on web browser. Appletviewer is provided to ensure that these programs run without a web browser. Servlets. These are programs that run at the server side.

2.

Java as a Platform

A software and/or hardware environment in which a program runs is referred to as a platform. The Java platform is a software that is compatible with and/ or executes on platform based on different hardwares. The Java runtime environment is shown below: Source Code

Compiler Byte Code

Virtual Machine (Interpreter)

Object Code

Operating System

Hardware Run-time Environment for Java

Advanced Java (CSE-404-E)


The Java platform consists of the Java Virtual Machine and the Java application Programming Interface. These two important components of the Java platform can be analyzed as follows: Java Virtual Machine ( JVM ). It is an interpreter, which receives bytecode files and translates them into machine language (object code) instructions. The JVM is specific to each operating system (for example, UNIX, Linux, Windows). Java Application Programming Interface ( Java API ). This is a collection of software components that offer various useful functions. The API is grouped in the form of packages (libraries) of interrelated classes as well as interfaces.

1.1.1 Need for JAVA


Java is needed for the following many reasons: 1. Java is a platform independent programming language that enables us to compile an application on one platform and execute it on any platform. 2. Java is a software independent programming language as its applications can be run on any operating system. 3. Java is a hardware / device independent programming language such that it can work everywhere, from the smallest device, such as microwave ovens and remote controls to supercomputers. 4. Java programming language enables us to develop an Internet based applications that can be accessed by programmers working on various types of computers. 5. Java provides a runtime environment, which implements various security checks on Java applets or applications and does not allow them to perform any malicious tasks. To develop an efficient application with above mentioned features, Java is needed.

1.1.2 Characteristics of JAVA


Java has unrivalled features that make it popular as a programming language. It is simple, is highly robust, secure, platform-independent, and hence portable. These features of Java are given below: 1. Java is a simple and object oriented language. A Java programmer does not need to know about the details of Java as to how memory is allocated to data, because in Java, the programmer does not need to handle memory manipulations. Java is a true object-oriented programming language. Almost everything in Java is an object and data reside within objects and classes. Java supports 3

Advanced Java (CSE-404-E)


various features of object-oriented programming language, such as, abstraction, encapsulation, inheritance and polymorphism. 2. Java is Highly Secure. Java allows programmers to develop (or create) highly reliable software applications. It provides extensive compile-time checking, followed by a second level of run-time checking. Java technology is designed to operate in distributed environments, which means that security is of paramount importance. 3. Java is Portable / Platform Independent. Portability refers to the ability of a program to run on any platform without changing the source code of the program. The programs developed on one computer can run on another computer which might have a different platform. Java enables the creation of cross-platform programs by compiling the program into an intermediate code called Java Byte Code. Source Code JVM Compiler Byte Code In JAVA

HOST

Byte Code

LOCAL
Machine Language

JVM Interpreter

In C 4. Java Shows High Performance. Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code. Javas architecture is designed to reduce overheads during runtime. Incorporation of multi-threading enhances the overall execution speed of Java programs. 5. Java is Compiled and Interpreted. The Java programs are first compiled and then interpreted. While compiling, the compiler checks for the errors in the program and list them all on the screen. After error-correction,

Advanced Java (CSE-404-E)


the program is recompiled and then the compiler converts the program into computer language. The Java compiler compiles the code to a Byte code that is understood by Java. When Java source code file (.java) is compiled, the Java compiler generates the Bytecode, which is a compiled Java program with a .class extension. The Java Virtual Machine (JVM) then interprets the Byte code into the computer code and runs it. While interpreting the interpreter software reads and executes the code line by line. 6. Java is Distributed. Java is used to develop applications that can be distributed among various computers on the network. Java applications can open and access remote objects on the Internet in the same manner as they can open and access objects in a local network. 7. Multithreaded and Interactive. Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs. For example, we can listen to an audio clip while scrolling a page and at the same time download an applet from a distant computer. This feature greatly improves the interactive performance of graphical applications. 8. Java is Dynamic and Extensible. Java is a dynamic language. It is capable of dynamically linking in new class libraries, methods, and objects. While the Java compiler is strict in its compile-time static checking, the language and run-time system are dynamic in their linking only as needed. New code modules can be linked in on demand from a variety of sources, even from sources across a network. 9. Java is Software Independent. Java is a software independent language. An application compiled in Java can be executed with another software using JVM. 10.Java is Hardware / Device Independent. Java is a hardware / device independent programming language such that it can work everywhere from the smallest device, such as microwave oven, and remote controls to supercomputers.

1.2 The Building Blocks of JAVA


Java, as a programming language, follows a set of rules that define any programming language. These are predefined conventions that specify how syntactically legal constructs can be formed using the language elements.

1.2.1 Lexical Tokens


Low level language elements are called Lexical Tokens. These tokens are building blocks for more complex constructs.

Advanced Java (CSE-404-E)


For example, identifiers, constants, operators and special characters that can be used to build high level constructs such as expressions, statements, methods and classes. Identifiers: Program elements such as a variable, function/method, class and so on. Java has certain rules for naming identifiers. The naming rules are given below: Digits, letters, underscore or currency symbols ($, , , ) are allowed. The first letter of any identifier cannot be a digit. An identifier nname must not be a reserved word. Java is case sensitive, label and Label are two different identifiers.

Keywords: Programming languages have certain words, usually termed reserved words or keywords, that convey special meanings to the compiler. These keywords can be used only for their intended action and they cannot be used for any other purpose.

1.2.2 Literals
A literal denotes constant value. This implies that 0 (zero) is an integer literal and c is a character literal. Reserved literals true and false are used to represent boolean literals. A simple Java program is a string literal. In addition there is the null literal (null) which represents the null reference. Integer Literals: primary literals used in Java programming. Integer literals can be of decimal, hexadecimal and octal formats. Floating-point literals: represent decimal numbers with fractional parts, such as 3.145. They can be expressed in either standard or scientific notation, meaning that the number 563.84 can also be expressed as 5.6384e2. Boolean Literals: commonly used in Java programming, present in almost every type of control structure. Boolean literals are needed to represent a condition or state that has only two possible values. Boolean values can be denoted using the reserved literals true or false. Character Literals: Unicode is a universal character encoding representation. It uses two bytes (16-bit character set) and also represents all international languages as character sets in Java. String Literal: is a sequence of characters, that are given within quotation marks and must occur on a single line.

Advanced Java (CSE-404-E)

1.2.3 White Spaces


A white space is a sequence of spaces, tabs, form feeds and line-terminator characters. Like terminators can be newline, carriage return newline sequences in a Java source file. A Java program is a free-format sequence of characters, which is tokenized by the compiler, i.e., broken into stream of tokens for further analysis. Separators and operators can help to distinguish tokens, but sometimes white spaces can be generated in a program. Types of white spaces: White Space position Space Tab New-Line Carriage-return Representation \t \n \t or \0 No. of Spaces/cursor Single Space 4 spaces Starting position in the newline Current position in the newline

1.2.4 Comments
A program can be documented by inserting comments at relevant places. The Java compiler ignores these comments. Java provides three types of comments: Single line comments: All characters that follow // until end of the line constitute a single line comment. For example: // This comment ends at the end of this line. Multiple line comments: A multiple line comment can spanseveral lines. Such a comment starts with /* and ends with */. For example: /* A comment on several lines */ Documentation Comment: A documentation comment is a special purpose comment which when placed at appropriate places in the program can be extracted and used by the javadoc utility to generate HTML documentation for the program. Documentation comments are usually placed in front of class, interface, method, and variable definitions. Groups of

Advanced Java (CSE-404-E)


special tags can be used inside a documentation comment to provide more specific information. Such a comment starts with /** and ends with */. For example, /** * This class implements open an account * @author PRRao. * @version 1.0 */ Comments cannot be nested. The comment start sequences (//, /*, /**) are not treated differently from other characters when occurring within comments.

1.3 Data Types


A data type defines a set of values and the operations that can be performed on them. Unlike C++, Java is a strongly typed language, which means that Java gives importance to type checking. The various data types in Java are: Primitive or the simple data types Abstract or the derived data types

1.3.1 Primitive Data Types


The built in data types in Java are known as the primitive or the simple data types There are eight primitive data types, of which seven are as shown below: Data Types Maximum Value byte short int long char float double Width (in bytes) Minimum Value 1 2 4 8 2 4 8 -27 -215 -231 -263 0x0 1.401298e-45 3.940656e-324 27 1 215 1 231 1 263 1 0xffff 3.402823e+38 1.797693e+308

The eight primitive data types can further be grouped in the following categories:

1.3.1.1

Integer Type

Advanced Java (CSE-404-E)


Integer type can store whole number values. The size of the values of the variables depends upon the chosen integer data type. The four integer data types are bytes, short, int and long. They represent signed integers. Some examples of defining integer data types are given below: int i = - 215 int max = 0x7fffffff (hexadecimal integer literal with value of 2,147,483,647) int min=0x80000000 (hexadecimal integer literal with value of -2,147,483,648) long distance = 05402202647L (octal long literal) long zipcode = 55584152L (long literal)

1.3.1.2

Character Type

Character data type can store symbols, such as letters and numbers. In Java, the character data type is represented by char. The char data type represents symbols in the unicode character set, such as letters, digits, and special characters. It encompasses all the 65536 (216) characters in the Unicode character set which are written in the form of 16 bit values. The first 128 characters of the Unicode set (0 127) are the same as the 128 characters of the 7 bit ASCII character set, and the first 256 characters of the Unicode set (0 - 255) correspond to the 256 characters of the 8 bit ISO Latin-I character set.

1.3.1.3

Floating Point Type

Floating point data type can store fractional numbers. This category includes the data types float and double. Examples of these are the following: float pi = 3.14159F double p = 313.159e-2 double fraction = 1.0/3.0

1.3.1.4

Boolean Type

Boolean data types is used to represent logical values that can be either true or false. All relational, conditional, and logical operators return boolean values. They are primarily used to keep track of the flow during program execution. Boolean values cannot be converted to other primitive data types, and vice versa. Primitive data values are atomic and are not objects. Each primitive data type defines the range of values in the data type and special operators Note: The groups, Integer and Floating point can store negative values. Java, unlike C/C++, does not support the concept of unsigned integers.

Advanced Java (CSE-404-E)

1.3.2 Abstract Data Types


The abstract data types include the data types derived from the primitive data types and have more functions than primitive data types. For example, String is an abstract data type that can store letters, digits, and other characters, such as /, (), ;, :, $ and #. You cannot perform calculations on a variable of the string data type even if the data stored in it has digits. However, String provides methods for concatenating two strings, searching one string within another, and extracting a part of a string.

*Further do it yourself

10

You might also like