Java Chapter 1 - Basics 06-19-2013, 06:01 PM
#1
What is Java.
Java is an object oriented programming language, which was designed by James Gosling at Sun Microsystems in 1995. This language is mostly based on C/C++, but it is still less powerful than either of them. You can run Java program on any OS.
A Java program is called source code and the JVM [Java Virtual Machine] is responsible for the conversion of the source code into Bytecode [classfile].
Java is one of the most popular programming language, why?
1. Write once run anywhere.
2. You can create powerful web apps.
3. You can develop server side apps for forums.
4. Java has the MOST open source library to choose from.
Etc.
Hence, in Java all source code of a program is first written and is saved in text form, then the Java compiler [javac] does its job by converting the source code into object code or Bytecode as class file, after that JVM runs the class file containing the Bytecode and as we know that JVM is available on many OS's so virtually a .class file can be run on almost any operating system.
So, if you hand the same Java Bytecode file to a Macintosh, then the Macintosh JVM interprets that same Bytecode for the Mac environment and if you hand the bytecode to Windows then Windows JVM will interpret it for Windows environment.
Getting Your Computer Ready To Write Java Programs.
To start writing Java programs we need some tools to get started, those are:
1. Java compiler.
2. Java Virtual Machine.
3. Java API
4. Access to the Java API documentation.
5. An editor to compose your Java programs. You can use WordPad or notepad but we are going to use an IDE.
-->For the first 4 things we just need Java SDK which can be downloaded from here:-
http://www.oracle.com/technetwork/java/j...80260.html
Choose your flavor and system type(32bit or 64bit) and then download and install.
-->For editing the programs we will use an Integrated development environment or IDE, we are going to use IDE because it’s a better way to write Java programs. Here are some reasons why it’s preferred way of making Java Programs.
• It divides your screen into many panes so that you can edit, issue commands and test the programs with an ease.
• You can customize the panes for quick access.
• Run, Debug and compile your program with just a click.
There are a lot of IDE’s but I am going to use Netbeans IDE, you can use another one called Eclipse.
Download:
Netbeans:
https://netbeans.org/downloads/index.html
Eclipse:
http://www.eclipse.org/downloads/
NOTE: If you are downloading 64bit java then download the 64bit version of IDE, same goes for 32bit.
Installing IDE and Running a Simple Java Program
Now, after installing your favorite IDE, open it.
Run Your First Program.
Step1: Click on “File” on the menu bar and then click on “New Project”
Step2: For the categories select “Java” and Projects select “Java Application” and click on Next.
Step3: Now enter “Project Name” and click on finish.
Step4: Now, you will see the project pane populated and the source code pane will have the source file “you-project-name.java” opened.
Step5: After the following code:-
public static void main(String[] args) {
add:-
System.out.println(“Hello World”);
And then run your program, if you see “Hello World” printed in the output pane, then the IDE works. And yes I forgot to add that Java like C is a case sensitive language so in Java a is not equal to A. So, watch out for cases.
Some Terms in Java Programs
Now, we are going to talk about some important terms in our java program, those are Package and Classes. We will talk about System.out.println(); in next chapter and public static void main(String[] args) in further chapters.
Package:
The group of types like classes, interfaces, enumerations and annotations is called package, in short it contains all the files related to your programs.Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc.
Some of the existing packages in Java are:
• java.lang - bundles the fundamental classes
• java.io - classes for input , output functions are bundled in this package
Also, programmers can create their own classes. Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classed.
Classes
Java class file is a file (with the .class filename extension) containing a Java bytecode which can be executed on the Java Virtual Machine (JVM). As mentioned earlier it is generated by javac from the .java file or your program’s source code.
Important Points
1. A Java program can be run on any platform, since it is Write once run anywhere.
2. A Java program is first converted into Bytecode (object code) by JVM and then is run on the computer.
3. Package is a group of related types like classes, interfaces, enumerations and annotations.
4. Java Class file contain the Bytecode generated by the Java virtual machine.
5. I recommend you to remember the following line:-
package packagename;
public class packagename {
public static void main(String[] args) {
}
}
In case IDE is not available, it will be very useful.
Java is an object oriented programming language, which was designed by James Gosling at Sun Microsystems in 1995. This language is mostly based on C/C++, but it is still less powerful than either of them. You can run Java program on any OS.
A Java program is called source code and the JVM [Java Virtual Machine] is responsible for the conversion of the source code into Bytecode [classfile].
Java is one of the most popular programming language, why?
1. Write once run anywhere.
2. You can create powerful web apps.
3. You can develop server side apps for forums.
4. Java has the MOST open source library to choose from.
Etc.
Hence, in Java all source code of a program is first written and is saved in text form, then the Java compiler [javac] does its job by converting the source code into object code or Bytecode as class file, after that JVM runs the class file containing the Bytecode and as we know that JVM is available on many OS's so virtually a .class file can be run on almost any operating system.
So, if you hand the same Java Bytecode file to a Macintosh, then the Macintosh JVM interprets that same Bytecode for the Mac environment and if you hand the bytecode to Windows then Windows JVM will interpret it for Windows environment.
Getting Your Computer Ready To Write Java Programs.
To start writing Java programs we need some tools to get started, those are:
1. Java compiler.
2. Java Virtual Machine.
3. Java API
4. Access to the Java API documentation.
5. An editor to compose your Java programs. You can use WordPad or notepad but we are going to use an IDE.
-->For the first 4 things we just need Java SDK which can be downloaded from here:-
http://www.oracle.com/technetwork/java/j...80260.html
Choose your flavor and system type(32bit or 64bit) and then download and install.
-->For editing the programs we will use an Integrated development environment or IDE, we are going to use IDE because it’s a better way to write Java programs. Here are some reasons why it’s preferred way of making Java Programs.
• It divides your screen into many panes so that you can edit, issue commands and test the programs with an ease.
• You can customize the panes for quick access.
• Run, Debug and compile your program with just a click.
There are a lot of IDE’s but I am going to use Netbeans IDE, you can use another one called Eclipse.
Download:
Netbeans:
https://netbeans.org/downloads/index.html
Eclipse:
http://www.eclipse.org/downloads/
NOTE: If you are downloading 64bit java then download the 64bit version of IDE, same goes for 32bit.
Installing IDE and Running a Simple Java Program
Now, after installing your favorite IDE, open it.
Run Your First Program.
Step1: Click on “File” on the menu bar and then click on “New Project”
Step2: For the categories select “Java” and Projects select “Java Application” and click on Next.
Step3: Now enter “Project Name” and click on finish.
Step4: Now, you will see the project pane populated and the source code pane will have the source file “you-project-name.java” opened.
Step5: After the following code:-
public static void main(String[] args) {
add:-
System.out.println(“Hello World”);
And then run your program, if you see “Hello World” printed in the output pane, then the IDE works. And yes I forgot to add that Java like C is a case sensitive language so in Java a is not equal to A. So, watch out for cases.
Some Terms in Java Programs
Now, we are going to talk about some important terms in our java program, those are Package and Classes. We will talk about System.out.println(); in next chapter and public static void main(String[] args) in further chapters.
Package:
The group of types like classes, interfaces, enumerations and annotations is called package, in short it contains all the files related to your programs.Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc.
Some of the existing packages in Java are:
• java.lang - bundles the fundamental classes
• java.io - classes for input , output functions are bundled in this package
Also, programmers can create their own classes. Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classed.
Classes
Java class file is a file (with the .class filename extension) containing a Java bytecode which can be executed on the Java Virtual Machine (JVM). As mentioned earlier it is generated by javac from the .java file or your program’s source code.
Important Points
1. A Java program can be run on any platform, since it is Write once run anywhere.
2. A Java program is first converted into Bytecode (object code) by JVM and then is run on the computer.
3. Package is a group of related types like classes, interfaces, enumerations and annotations.
4. Java Class file contain the Bytecode generated by the Java virtual machine.
5. I recommend you to remember the following line:-
package packagename;
public class packagename {
public static void main(String[] args) {
}
}
In case IDE is not available, it will be very useful.