Você está na página 1de 23

JAVA

CORE JAVA

Satyam Info-Tech Since 2006

Java What Is Java?


Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end Unix workstations. Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level. Java is often mentioned in the same breath as Hot Java, a World Wide Web browser from Sun like Netscape or Mosaic (see Figure 1). What makes Hot Java different from most other browsers is that, in addition to all its basic Web features, it can also download and play applets on the readers system. Applets appear in a Web page much in the same way as images do, but unlike images, applets are dynamic and interactive. Applets can be used to create animations, figures, or areas that can respond to input from the reader, games, or other interactive effects on the same Web pages among the text and graphics. Although Hot Java was the first World Wide Web browser to be able to play Java applets, Java support is rapidly becoming available in other browsers. Netscape 2.0 provides support for Java applets, and other browser developers have also announced support for Java in forthcoming products. Java is general purpose object oriented programming language developed by sun micro systems of USA in 1991 originally called OAK by JAMES GOSLING. Java was designed for software for consumer electronic services like TVs, VCRs, Toasters, and such other electronic machines the good has a strong impact on the development to make the language. Simple, portable and highly reliable in the software industries today is the chain, change is the one of the most critical aspects of software development and management many Program in approaches have been tried this included techniques such as Modular Programming, Top down Programming, Bottom up Programming and Structured Programming this structure programming provide to be a powerful tool that enabled programmers to write complex programs fairly easily.

Why Learn Java?


At the moment, probably the most compelling reason to learn Javaand probably the reason you bought this bookis that Hot Java applets are written in Java. Even if that were not the case, Java as a language has significant advantages over other languages and other programming environments that make it suitable for just about any programming task. This section describes some of those advantages.

Page | 2

Satyam Info-Tech Since 2006

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)

Binary File (SPARC)

Compiler (SPARC)

Page | 3

Satyam Info-Tech Since 2006


system, the resulting program will run only on other Pentium systems. If you want to use the same program on another system, you have to go back to your original source, get a compiler for that system, and recompile your code. Figure 1.2 shows the result of this system: multiple executable programs for multiple systems. Things are different when you write code in Java. The Java development environment has two parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program and instead of generating machine codes from your source files, it generates byte codes. To run a Java program, you run a program called a byte code interpreter, which in turn executes your Java program (see Figure 1.3). You can either run the interpreter by itself, orfor appletsthere is a byte code interpreter built into Hot Java and other Java-capable browsers that runs the applet for you.
Java Interpreter (Pentium)

Figure 1.2 Java programs.

Java Code
Java Compiler (Pentium)

Java Bytecode (Platform Independent)

Java Interpreter (PowerPC)

Java Compiler (PowerPC)

Java Interpreter (SPARC)

Java Compiler (SPARC)

Java Is Easy to Learn


In addition to its portability and object-orientation, one of Javas initial design goals was to be small and simple, and therefore easier to write, easier to compile, easier to debug, and, best of all, easy to learn. Keeping the language small also makes it more robust because there are fewer chances for programmers to make difficult-to-find mistakes. Despite its size and simple design, however, Java still has a great deal of power and flexibility. Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you, because you have most of the foundation already.

Page | 4

Satyam Info-Tech Since 2006


Although Java looks similar to C and C++, most of the more complex parts of those languages have been excluded from Java, making the language simpler without sacrificing much of its power. There are no pointers in Java, nor is there pointer arithmetic. Strings and arrays are real objects in Java. Memory management is automatic. To an experienced programmer, these omissions may be difficult to get used to, but to beginners or programmers who have worked in other languages, they make the Java language far easier to learn.

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

Beginners Java Tutorials - Installing JDK


Downloading and Installing J2SE Software on Windows Platform To download J2SE for development visit http://www.java.sun.com/j2se and download J2SE on your machine. In this tutorial we have used jdk-1_5_0_06-windows-i586.exe.The java 2Platform or (JDK) can be downloaded from the sun. Formerly Known as the java Development kit ,or JDK, Downloading java is really about downloading the java 2 plat form that comes in three editions , J2ME, J2SE and J2EE , if you are learning java, then, you should start by downloading J2EE. Once you have downloaded the j2se on your system, you are ready to install . In the following section we will learn how to install jdk development environment on your machine. here are the step to install JDK on your windows machine.

Page | 5

Satyam Info-Tech Since 2006


Step 1 Double click the JDK down loaded file, the executable extracts the required Contents to the temporary directory and then License agreement screen appears. On the license agreement page read and accept the license and the click the next button .

Step 2 The custom setup screen appears as follows.

Page | 6

Satyam Info-Tech Since 2006

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

Satyam Info-Tech Since 2006

and click on the "OK" button. After clicking on the "OK" button installation begins:

Page | 8

Satyam Info-Tech Since 2006

Step 4 In the next window installer asks for the installing the runtime as shown in the following screen:

Page | 9

Satyam Info-Tech Since 2006

Step 5 Click on next button install the J2SE runtime on your machine. Next screen shows the browser selection:

Page | 10

Satyam Info-Tech Since 2006

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

Satyam Info-Tech Since 2006

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

Satyam Info-Tech Since 2006

and add "c:\jdk1.5.0_06" to path variable:

Page | 13

Satyam Info-Tech Since 2006

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

Satyam Info-Tech Since 2006


To some, object-oriented programming (OOP) technique is merely a way of organizing programs, and it can be accomplished using any language. Working with a real object-oriented language and programming environment, however, enables you to take full advantage of object-oriented methodology and its capabilities of creating flexible, modular programs and reusing code. Many of Javas object-oriented concepts are inherited from C++, the language on which it is based, but it borrows many concepts from other object-oriented languages as well. Like most objectoriented programming languages, Java includes a set of class libraries that provide basic data types, system input and output capabilities, and other utility functions. These basic classes are part of the Java development kit, which also has classes to support networking, common Internet protocols, and user interface toolkit functions. Because these class libraries are written in Java, they are portable across platforms as all Java applications are.

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.

Creating a Java Application


Lets start by creating a simple Java application: the classic Hello World example that all language. As with all programming languages, your Java source files are created in a plain text editor, or in an editor that can save files in plain ASCII without any formatting characters. On UNIX, emacs, ped, or vi will work; on Windows, Notepad or DOS Edit are both text editors. Fire up your editor of choice, and enter the Java program shown in Listing 1.1. Type this program, as shown, in your text editor. Be careful that all the parentheses, braces, and quotes are there.
Listing 1.1. Your first Java application.

class HelloWorld { Public static void main (String args []) { System.out.println(Hello World!); } }

This program has two main parts:


Page | 15

Satyam Info-Tech Since 2006


The entire program is enclosed in a class definitionhere, a class called HelloWorld. The body of the program (here, just the one line) is contained in a routine called main(). In Java applications, as in a C or C++ program, main() is the first routine that is run when the program is executed. Youll learn more about both these parts of a Java application as the book progresses. Once you finish typing the program, save the file. Conventionally, Java source files are named the same name as the class they define, with an extension of .java. This file should therefore be called HelloWorld.java. Now, lets compile the source file using the Java compiler. In Suns JDK, the Java compiler is called javac. To compile your Java program, Make sure the javac program is in your execution path and type javac followed by the name of your source file: javac HelloWorld.java

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

Satyam Info-Tech Since 2006


3) Distributed Information is distributed on various computers on a network. Using Java, we can write programs, which capture information and distribute it to the clients. This is possible because Java can handle the protocols like TCP/IP and UDP. 4) Robust Robust means strong. Java programs are strong and they dont crash easily like a C or C++ program. There are two reasons for this. a) Java has got excellent in-built exception handling features. NEW Exception is an error that occurs at run time. TERM If an exception occurs, the program terminates abruptly giving rise to problems like loss of data. Overcoming such problems is called exception handling. This means that even though an exception occurs in a Java program, no harm will happen. b) Memory management features Most of the C and C++ programs crash in the middle because of not allocating sufficient memory or forgetting the memory to be freed in a program. Such problems will not allocate or deallocate the memory in Java. Everything will be taken care of by JVM only. For example, JVM will allocate the necessary memory needed by a Java program.

Java Virtual Machine


JVM is also capable of deallocating the memory when it is not used. A variable or an object is created in the memory and is not used; it is automatically removed by the garbage collector of JVM. NEW TERM Garbage collector is a form of memory that checks the memory from time to time and marks the variables or objects not used by the program automatically.

5) Secure Security problems like eavesdropping, tampering, impersonisation and virus threats can be eliminated or minimized by using Java on internet. Page | 17

Satyam Info-Tech Since 2006


6) System independence Javas bytecode is not machine dependent. It can be run on any machine with any processor and any operating system. 7) Portability If a program yields the same result on every machine, then that program is called portable. Java programs are portable. This is the result of Javas System independence nature. 8) Interpreted Java programs are compiled to generate the bytecode. This byte code can be downloaded and interpreted by the interpreter in JVM. If we take any other language, only an interpreter or a compiler is used to execute the programs. But in Java, we use both compiler and interpreter for the execution. 9) High performance The problem with interpreter inside the JVM is that it is slow. Because of this, Java programs used to run slow. To overcome this problem, along with the interpreter, JavaSoft people have introduced JIT (Just In Time) compiler, which enhances the speed of execution. So, now in JVM, both interpreter and JIT compiler work together to run the program. 10) Multithreaded A thread represents an individual process to execute a group of statements, JVM uses several threads to execute different blocks of code, creating multiple threads is called multithreaded. 11) Scalability Java platform can be implemented on a wide range of computers with varying levels of resources from embedded devices to mainframe computers. This is possible because Java is compact and platform independent. 12) Dynamic Before the development of Java, only static text used to be displayed in the browser. But when James Gosling demonstrated an animated atomic molecule where the rays are moving and stretching, the viewers were dumbstruck. This animation was done using an applet program, which are the dynamically interacting programs on internet.

Page | 18

Satyam Info-Tech Since 2006


JIT
The execution engine contains interpreter and JIT (Just In Time) compiler which are responsible for converting the byte code instruction into machine code. The JVM implements use of both the interpreter and JIT compiler to convert the byte code. This technique is also called adaptive optimizer. The JIT compiler will execute the looping instructions. First of all, the JIT compiler reads the print instruction and converts that into machine code. The JIT compiler allots a block of memory and pushes the machine code instruction into the memory. Both the interpreter and JIT compiler will work simultaneously to translate the byte code into machine code. JIT compiler is a part of JVM which increases the speed of execution of Java program.

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

Satyam Info-Tech Since 2006


e.g.: float i=3.142F; 3. Character data type This data type represents a single character like 9, P, *, etc. DATA TYPE Char MEMORY SIZE 2 bytes MINIMUM VALUES 0 to 655535

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

Satyam Info-Tech Since 2006


3. Character literals General characters, like A, b, 9, etc. Special characters, like ?, @, etc. Unicode characters, like \110042 (this represents a in 150 latin 1 character set). Escape sequence (backslash codes) like \n, \b, etc.

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

Satyam Info-Tech Since 2006


Decrement operator (--) This operator is used to decrement the value of a variable by 1. Int x=1; --x will make x=0 xnow, x= -1 3. Assignment operator This operator is used to store some value into a variable. This can be done in three ways: It is used to store a value into a variable int x=5; It is used to store the value of a variable into another variable Int x=y; //where the value of y is stored into x It is used to store the value of an expression into a variable Int x=y+z-4;//here y+z-4 is stored in x

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

Satyam Info-Tech Since 2006


4. Logical operators Logical operators are used to construct compound conditions. && and operator || or operator ! not operator

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

Você também pode gostar