Escolar Documentos
Profissional Documentos
Cultura Documentos
CORE JAVA
Page | 2
Java Is Platform-Independent
Platform independence is one of the most significant advantages that Java has over other programming languages, particularly for systems that need to work on many different platforms. Java is platform-independent at both the source and the binary level. NEW Platform-independence is a programs capability of moving easily from one computer system to another. TERM At the source level, Javas primitive data types have consistent sizes across all development platforms. Javas foundation class libraries make it easy to write code that can be moved from platform to platform without the need to rewrite it to work with that platform. Platform-independence doesnt stop at the source level, however. Java binary files are also platform independent and can run on multiple problems without the need to recompile the source. How does this work? Java binary files are actually in a form called byte codes. NEW specific to any one processor. TERM Normally, when you compile a program written in C or in most other languages, the compiler translates your program into machine codes or processor instructions. Those instructions are specific to the processor your computer is runningso, for example, if you compile your code on a Pentium
Figure 1 Traditional compiled Programs.
Binary File (Pentium)
Byte codes are a set of instructions that looks a lot like some machine codes, but that is not
Youre Code
Compiler (Pentium) Binary File (PowerPC)
Compiler (PowerPC)
Compiler (SPARC)
Page | 3
Java Code
Java Compiler (Pentium)
Page | 4
History of Java
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2002 2004 Sun micro systems decided to develop special software Object oriented language C ++ the team announced the language named OAK. Green project team by sun demonstrated the application of the new language to control a list of home appliances www (World Wide Web) appeared on the internet and transformed test based internet in to a graphical rich environment. Web browser called web java to locate and run applet programming. Oak was renamed with java Java development kit 1.0 JDK Java to with version 1.2 of the Software Development Kit Java to plat form Standard Additional J2SE and Enter Praised Edition J2EE J2SE with SDK-1.3 J2SE with SDK-1.4 J2SE with JDK-5.0
Page | 5
Page | 6
Step 3 Click on the change button to change the installation directory to "c:\jdk1.5.0_06" as shown in the following screen.
Page | 7
and click on the "OK" button. After clicking on the "OK" button installation begins:
Page | 8
Step 4 In the next window installer asks for the installing the runtime as shown in the following screen:
Page | 9
Step 5 Click on next button install the J2SE runtime on your machine. Next screen shows the browser selection:
Page | 10
Click on the "Next" button. Step 6 Once the installation is finished it shows you the final screen indications the success. Now you have successfully installed J2SE on your machine. Installer shows the following final confirmation window as shown below:
Page | 11
Click on the "Finish" button to exit from the installer. Configuring the installation on windows machine: In this Section we will add some settings to the windows environment so that the java compiler and runtime becomes available for compiling and running the java application. Go to the control panel and double click on "System Properties" and to to the advance tab.
Page | 12
Page | 13
and click on ok button. To save the setting click on "OK" button. This will make the java environment available for development. Open the dos prompt and type javac on the console, it should show the following output:
TYPES OF PROGRAMMING
There are several types of programming. 1) Console-based 2) Windows-based 3) Procedural 4) Object-oriented 5) Event-driven
Java Is Object-Oriented
Page | 14
Programming in Java
Enough background! Lets finish off this day by creating two real Java programs: a stand-alone. Java application and an applet that you can view in either in the applet viewer (part of the JDK) or in a Java-capable browser. Although both these programs are extremely simple, they will give you an idea of what a Java program looks like and how to compile and run it.
class HelloWorld { Public static void main (String args []) { System.out.println(Hello World!); } }
Comments
Comments are sections of a program that are ignored by the compiler and are used to document the program. Documentation is important in that it provides information about the program that is useful in the maintenance of the program. The only comment that is common to C, C++ and Java is the Single-line & multi-line comment. The comment begins with a / ends with a * and * /. Everything between these two sets of symbols is treated as a comment and is ignored by the compiler. The comment begins with a // called Single-line.
Features
1) Simple Java is a simple programming language. It has to work on electronic devices where less memory is available. 2) Object oriented programming language Java is an object oriented programming language. Java programs use objects and classes. An object is anything that really exists in the world. Everything that we see physically will come into the definition. A group of objects exhibiting same behavior will come under the same group called a class.
Page | 16
5) Secure Security problems like eavesdropping, tampering, impersonisation and virus threats can be eliminated or minimized by using Java on internet. Page | 17
Page | 18
Data Types
1. Integer data types These data types represent integer numbers, numbers without any fractional parts or decimal points. DATA TYPES Byte Short Int Long E.g.: Byte rno=10; 2. Float data types These data types are useful to represent numbers with decimal point. DATA TYPE Float Double MEMORY SIZE 4 bytes 8 bytes MINIMUM AND MAXIMUM VALUES -3.4e38 to -1.4e-45 for negative values and 1.4e-45 to 3.4e38 for positive values -1.8e308 to -4.9e-324 for negative values and 4.9e-324 to 1.8e308 for positive values Page | 19 MEMORY SIZE 1 byte 2 bytes 4 bytes 8 bytes MINIMUM AND MAXIMUM VALUES -128 to +127 -32768 to +32767 -2147483648 to +2147483647 -9223372036854775808 to +9223372036854775807
4. String data types A string represents a group of characters, like New Delhi, AP123, etc. String str=New Delhi; 5. Boolean data types Boolean data types represent any of the two values true or false. JVM uses 1 bit to represent a Boolean value internally. e.g.: Boolean response=true;
Literals
A literal represents a value that is stored into a variable directly in the program. 1. Integer literals int.decval=26;//the no. 26 in decimal int.octval=032;//the no. 26 in octal int.hexval=0x1a;//the no. 26 in hexadecimal 2. Float literals double d1=123.4; double d2=1.234e2;//same value as d1, but in scientific notation float f1=123.4F;
Page | 20
4. String literals String literals represent objects of string class. e.g. Hello, Anil Kumar, AP1201, etc. 5. Boolean literals Boolean literals represent only two values- true & false
Operators
1. Arithmetic operators +, -, /, *, % 2. Unary operators Unary minus operator (-) Increment operator (++) Decrement operator (--) Increment operator (++) Int x=1; ++x will make x=2 ++x now x=3 Value of x is incremented by 1 when ++operator is used
Page | 21
Compact notationx+=10 addition assignment operator sal*=10.5 multiplication assignment operator value-=discount subtraction assignment operator 8/=1000 division assignment operator Num%=5.5 modulus assignment operator 3. Relational operator Relational operator is used for comparing >(greater than) <(less than) >=(greater than or equal than) <=(less than or equal to) ==(equal to) !=(not equal to) Page | 22
5. Boolean operators & Boolean and operator | Boolean or operator ! Boolean not operator a=true; b=false; a&b=a and are true; a|b= a or b is true; a&a=a and a are true; b|b=b or b is true; !a=not a; !b=not b;
Page | 23