Docsity

Java Programming: History, Evolution, and Fundamental Concepts, Lecture notes of Java Programming

Toggle sidebar
bg1
Java History and Evolution:
Java is a general-purpose, object-oriented programming language developed
by Sun Microsystems of USA in 1991.Originally called Oak by James Gosling (one of
the inventor of the language). Java was invented for the development of software
for consumer electronic devices like TVs, toasters, etc. The main aim had to make
java simple, portable and reliable.
UNIT – I
History and Evolution of Java - Features of Java - Object Oriented
Concepts – Bytecode - Lexical Issues - Data Types – Variables- Type Conversion
and Casting- Operators - Arithmetic Operators - Bitwise - Relational Operators
- Assignment Operator - The conditional Operator - Operator Precedence-
Control Statements – Arrays.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Java Programming: History, Evolution, and Fundamental Concepts and more Lecture notes Java Programming in PDF only on Docsity!

Java History and Evolution: Java is a general-purpose, object-oriented programming language developed by Sun Microsystems of USA in 1991.Originally called Oak by James Gosling (one of the inventor of the language). Java was invented for the development of software for consumer electronic devices like TVs, toasters, etc. The main aim had to make java simple, portable and reliable.

UNIT – I

History and Evolution of Java - Features of Java - Object Oriented Concepts – Bytecode - Lexical Issues - Data Types – Variables- Type Conversion and Casting- Operators - Arithmetic Operators - Bitwise - Relational Operators

- Assignment Operator - The conditional Operator - Operator Precedence- Control Statements – Arrays.

Features of Java: I. Simple : Java was designed to be easy for professional programmers to learn, if he already knows C &C++. Java includes syntaxes from C and object oriented concepts from C++. The confusing concepts in both C & C++ are leftover here. So java is easy to learn and it is simple language. II. Secure : Security becomes an important issue for a language that is used for programming on internet. Threat of viruses and abuse of resources are everywhere. Java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in java ensures that programs can’t gain access to memory locations without proper authorization. III. Portable : The most significant contribution of java over other languages is its portability. Java programs can be easily moved from on computer to another, anywhere and anytime. Changes and upgrades in operating systems, processors and system resources will not force any changes in java programs. This is the reason why java has become popular language for programming on internet which interconnects different kinds of systems worldwide. IV. Object-oriented : java is a true object oriented language. Almost everything in java is an object. All program code and data reside with in objects & classes. Java comes with an extensive set of classes, arranged in packages, that we can use in our programs by inheritance. The object Model in java is simple and easy to extend. V. Robust : Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and runtime checking for data types. It is designed as a garbage collected language relieving the programmers virtually all memory management problems. Java also incorporates the concept of exception handling which captures serious errors and eliminates any risk of crash the system. VI. Multithreaded : It means handling multiple tasks simultaneously. Java supports multithreaded programs. This means that we need not wait for the application to finish one task before another. For eg, 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. VII. Architectural Neutral: One of the problems facing by programmers was program written today will not be run tomorrow even in the same machine or if the OS or if the processor upgrades. So java designers made it architectural neutral by implementing JVM (Java Virtual Machine)through java runtime environment. The main role of

OOPs (Object Oriented Programming System) Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: o Object o Class o Inheritance o Polymorphism o Abstraction o Encapsulation Object Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. Class Collection of objects is called class. It is a logical entity. Inheritance When one object acquires all the properties and behaviors of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. Polymorphism When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.

In java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something e.g. cat speaks meow, dog barks woof etc. Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction Encapsulation Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines. A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. BYTECODE: Java bytecode is the instruction set for the Java Virtual Machine. It acts similar to an assembler which is an alias representation of a C++ code. As soon as a java program is compiled, java bytecode is generated. In more apt terms, java bytecode is the machine code in the form of a .class file. With the help of java bytecode we achieve platform independence in java. How it works:

enclosed text should be included in automatically generated documentation as a description of the declared item. Identifiers Identifiers must start with a letter, underscore ("_"), or dollar sign ("$"); subsequent characters can also contain digits (0-9). Java uses the Unicode character set. For the purposes of determining what a legal identifier is the following are considered "letters:"  The characters "A" through "Z"  The characters "a" through "z"  All Unicode characters with a character number above hex 00C Other characters valid after the first letter of an identifier include every character except those in the segment of Unicode reserved for special characters. Thus, "garcon" and "Mjolnir" are legal identifiers, but strings containing characters such as "xa6 " are not. For more information on the Unicode standard, see The Unicode Standard, Worldwide Character Encoding, Version 1.0, Volumes 1&2. The FTP address for Unicode, Inc. (formerly the Unicode Consortium) is unicode.org. Keywords The following identifiers are reserved for use as keywords. They cannot be used in any other way.

Literals Literals are the basic representation of any integer, floating point, boolean, character, or string value. 1)Integer Literals Integers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) format. A decimal integer literal consists of a sequence of digits (optionally suffixed as described below) without a leading 0 (zero). An integer can be expressed in octal or hexadecimal rather than decimal. A leading 0 (zero) on an integer literal means it is in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7. Integer literals are of type int unless they are larger than 32-bits, in which case they are of type long (see Integer Types). A literal can be forced to be long by appending an L or l to its value. The following are all legal integer literals: 2, 2L 0777 0xDeadBeef 2) Floating Point Literals A floating point literal can have the following parts: a decimal integer, a decimal point ("."), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an e or E

5) String Literals A string literal is zero or more characters enclosed in double quotes. Each string literal is implemented as a String object (not as an array of characters). For example, "abc" creates an new instance of class String. The following are all legal string literals: "" \ the empty string """ "This is a string" "This is a
two-line string" Operators and Miscellaneous Separators The following characters are used in source code as operators or separators:

  • -! % ^ & * | ~ / > < ( ) { } [ ] ;? : ,. = In addition, the following character combinations are used as operators: ++ -- == <= >= != << >>

+= -= *= /= &=|= ^= %= <<= >>= >>>= || &&

Data Types Data types represent the different values to be stored in the variable. In java, there are two types of data types: o Primitive data types o Non-primitive data types DataType Default Value Defaultsize Boolean False 1 bit Char '\u0000' 2 byte Byte 0 1 byte Short 0 2 byte Int 0 4 byte Long 0L 8 byte Float 0.0f 4 byte Double 0.0d 8 byte

found in all human languages. Thus in java char is a 16bit type. The range of char is 0 to 65536. There are no negative chars. char variables are declared by use of char keyword. eg: char c; boolean: Java has a simple type called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators such as a<b. Boolean is also the type required by conditional expressions that govern the control statements such as ‘if’ and ‘for’. boolean variables are declared by use of ‘boolean’ keyword. eg: boolean b; b=false; Variables :  Java uses variables to store data.  To allocate memory space for a variable JVM requires:

  1. to specify the data type of the variable
  2. to associate an identifier with the variable
  3. optionally, the variable may be assigned an initial value All done as part of variable declaration. Basic Variable Declaration  datatype identifier [=value];  datatype must be  A simple datatype  User defined datatype (class type)  Identifier is a recognizable name confirm to identifier rules  Value is an optional initial value. Variable Declaration

We can declare several variables at the same time: type identifier [=value][, identifier [=value] …]; Examples: int a, b, c; int d = 3, e, f = 5; byte g = 22; double pi = 3.14159; char ch = 'x'; Variable Scope: Scope determines the visibility of program elements with respect to other program elements.  In Java, scope is defined separately for classes and methods:

  1. variables defined by a class have a global scope
  2. variables defined by a method have a local scope A scope is defined by a block: { … } A variable declared inside the scope is not visible outside: { int n; } n = 1;// this is illegal Variable Lifetime: Variables are created when their scope is entered by control flow and destroyed when their scope is left:

Type Casting: General form: (targetType) value  Examples:  1) Integer value will be reduced module bytes range: int i; byte b = (byte) i;  2) Floating-point value will be truncated to integer value: float f; int i = (int) f; Operators: Java supports a rich set of operators. Operators are used in programs to manipulate data and variables. Java operators can be classified into a number of related categories as below:

  1. Arithmetic operators
  2. Increment & Decrement operators
  3. Relational operators
  4. Bitwise operators
  5. Logical operators
  6. Assignment Operator
  7. Conditional Operator 1) Arithmetic operators: These are used in mathematical expressions in the same way that they are used in algebra. The following are the list of arithmetic operators:

Arithmetic operators are used as shown below: a-b a+b ab a/b a%b -ab (here a and b are operand) 2) Increment & Decrement operators: The ++ and the – are java’s increment and decrement operators. The increment operator increases its operand by one. The decrement operator decreases its operand by one. for example, x=x+1; can be written like x++; using increment operator similarly, x=x-1; can be written like x--; using decrement operator 3) Relational operators: The relational operators determine the relationship that one operand has to the other. The outcome of these operators is a boolean value. The relational operational operators are most frequently used in the expressions that control the if statement and the various loop statements. The following are the list of Relational operators:

6) Conditional Operator( ?:) The character pair ?: is a ternary operator available in Java. This operator is used to construct conditional expressions of the form exp1? exp2 : exp3 exp1 is evaluated first. It is nonzero (true), then the exp2 is evaluated and becomes the value of the conditional expression. if exp1 is false, exp3 is evaluated and its value becomes the value of the conditional expression. For example, a=10 , b=20; x = ( a > b )? a : b ; x will be assigned the value of b. This can be achieved using the if… else statement as follows: if(a > b) x = a; else x = b; Operator Precedence in Java: An arithmetic expression without any parentheses will be calculated from left to right using the rules of precedence of operators. There are two priority levels of arithmetic operators are as follows: (a) High priority (* / %) (b) Low priority (+ -) The evaluation process includes two left to right passes through the expression. During the first pass, the high priority operators are

applied as they are encountered. During the second pass, the low priority operators are applied as they are encountered. For example: Z=A-B/3+C3-1 When A=10, B=13, C= First pass: Z=10-(13/3) + (33)-1 Z=10-4+3- Second pass: Z=6+3-**