www.000webhost.com
/** * Created by PhpStorm. * User: Rahul * Date: 12/8/2016 * Time: 8:09 PM */ Core Java Java Tech Tutorials




Continue/ Next Page





Core Java - Getting Started


Core Java

History of java : -


Java Programming Language was conceived by the effort of 5 great people, James Gosling, Patrick Naughton, Chris Warth, Mike Sheridan and Ed Frank. They all worked for Sun Microsystems, Inc and came up with Java in 1991. The language took 18 months to develop and had a initial name as "Oak" which was renamed to Java in 1995, due to copyright issues.

The idea was to develop a language which was platform-independent and which could create embedded softwares for consumer electronic devices. C and C++ were quite inefficient for the purpose because they were not platform-independent as there programs have to be compiled for particular hardware before execution. Also, the compiled code was inefficient for other processors and it had to be recompiled. So the team of 5 also called as Green Team began to work in developing a easier and cost-efficient solution. They worked for 18 months in developing a portable, platform-independent language that could create a code which can run on variety of processors under differing environments.

The above necessity led to creation of Java. At the same time, Internet and WWW were becoming popular day-by-day. The web programs lacked the features of platform-independence. It required programs that could run on any operating system irrespective of hardware and software configuration. It required small and portable programs that could be securely transported over the network. The programming language available to suit such requirements was Java. Many developers soon realized that architectural neutral language like Java would be best for writing programs for internet. Thus forcus of Java was shifted from consumer electronics to World Wide Web. Today, Java is not an ordinary programming language. It is a technology which is simple, Object Oriented, Distributed, Robust, Secure, Architecture neutral, Portable, Interpreated, Multithreaded, High Performance and Dynamic.

How to install Java on Windows


JDK or JRE? JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit), which includes JRE plus the development tools (such as compiler and debugger), is need for writing as well as running Java programs. Since you are supposed to write Java Programs, you should install JDK, which includes JRE.
JDK Versions :-



after following below steps1 to step3 If you see a screen like below, then Java is Installed - for any issue comment here or contact us

javacTest.png

How to install Java :-







Getting Started - Write your First Hello-world Java Program



You should have already installed Java Development Kit (JDK). Otherwise, Read "How to Install JDK and Get Started with Java Programming". Let us begin by writing our first Java program that prints a message "Hello, world!" to the display console, as follows: Hello, world!

Step 1: Write the Source Code: Enter the following source codes using a programming text editor (such as TextPad or NotePad++ for Windows or gedit for UNIX/Lunix/Mac) or an Interactive Development Environment (IDE) (such as Eclipse or Netbeans - Read the respective "How-To" article on how to install and get started with these IDEs). Do not enter the line numbers (on the left panel), which were added to help in the explanation. Save the source file as "Hello.java". A Java source file should be saved with a file extension of ".java". The filename shall be the same as the classname - in this case "Hello".

 
		/*
		 * First Java program, which says "Hello, world!"
		 */
		public class Hello {   // Save as "Hello.java"
		   public static void main(String[] args) {
		      System.out.println("Hello, world!");   // print message
		   }
		}

Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable bytecode "Hello.class" using JDK compiler "javac". Start a CMD Shell (Windows) or Terminal (UNIX/Lunix/Mac) and issue this command:
javac Hello.java
javac is the name of JDK compiler. There is no need to explicitly compile the source code under IDEs (such as Eclipse or NetBeans), as they perform incremental compilation implicitly.

3: Run the Program: Run the program using Java Runtime "java", by issuing this command:
prompt> java Hello
Hello, world!
On IDEs (such as Eclipse or NetBeans), right-click on the source file and choose "Run As..." ⇒ "Java Application".

Brief Explanation of the Program
/* ...... */ // ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler. But they provide useful explanation and documentation to your readers (and to yourself three days later). There are two kinds of comments: Multi-line Comment: begins with /* and ends with */, and may span more than one lines (as in Lines 1-3). End-of-line Comment: begins with // and lasts until the end of the current line (as in Lines 4 and 6).
public class Hello { ...... }
The basic unit of a Java program is a class. A class called "Hello" is defined via the keyword "class" in Lines 4-8. The {...} is the body of the class. The keyword public will be discussed later. In Java, the name of the source file must be the same as the name of the public class with a mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".
public static void main(String[] args) { ...... }
Lines 5-7 defines the so-called main() method, which is the starting point, or entry point for program execution. The {...} is the body of the method which contains programming statements.
System.out.println("Hello, world!");
In Line 6, the statement System.out.println("Hello, world!") is used to print the message string "Hello, world!" to the display console.
A string is surrounded by a pair of double quotes and contain texts. The text will be printed as it is, without the double quotes.



JavaCompilerFlow_Process.png


Below is a simple Java program that demonstrates the three basic programming constructs: sequential, loop, and conditional.


/*
 * Sum the odd numbers and the even numbers from a lowerbound to an upperbound
 */
public class OddEvenSum {  // Save as "OddEvenSum.java"
   public static void main(String[] args) {
      int lowerbound = 1, upperbound = 1000;
      int sumOdd  = 0;    // For accumulating odd numbers, init to 0
      int sumEven = 0;    // For accumulating even numbers, init to 0
      int number = lowerbound;
      while (number <= upperbound) {
         if (number % 2 == 0) {  // Even
            sumEven += number;   // Same as sumEven = sumEven + number
         } else {                // Odd
            sumOdd += number;    // Same as sumOdd = sumOdd + number
         }
         ++number;  // Next number
      } 
      // Print the result
      System.out.println("The sum of odd numbers from " + lowerbound + " to " + upperbound + " is " + sumOdd);
      System.out.println("The sum of even numbers from " + lowerbound + " to " + upperbound + "  is " + sumEven);
      System.out.println("The difference between the two sums is " + (sumOdd - sumEven));
   }
}

-- Output --
The sum of odd numbers from 1 to 1000 is 250000
The sum of even numbers from 1 to 1000  is 250500
The difference between the two sums is -500



Comments :-



Comments are used to document and explain your codes and program logic. Comments are not programming statements and are ignored by the compiler, but they VERY IMPORTANT for providing documentation and explanation for others to understand your program (and also for yourself three days later).
There are two kinds of comments in Java:


I recommend that you use comments liberally to explain and document your codes. During program development, instead of deleting a chunk of statements irrevocably, you could comment-out these statements so that you could get them back later, if needed.


Statements :-



Statement: A programming statement is the smallest indepedent unit in a program, just like a sentence in the english language. It performs a piece of programming action. A programming statement must be terminated by a semi-colon (;), just like an english sentence ends with a period. (Why not ends with a period like an english sentence? This is beacuse period crashes with decimal point - it is hard for the dumb computer to differentiate between period and decimal point!)
For examples,
// Each of the following lines is a programming statement, which ends with a semi-colon (;)
int number1 = 10;
int number2, number3=99;
int product;
product = number1 * number2 * number3;
System.out.println("Hello");

Block :-



A block (or a compound statement) is a group of statements surrounded by curly braces { }. All the statements inside the block is treated as one single unit. Blocks are used as the body in constructs like class, method, if-else and for-loop, which may contain multiple statements but are treated as one unit. There is no need to put a semi-colon after the closing brace to end a compound statement. Empty block (no statement inside the braces) is permitted.
For examples :-

		// Each of the followings is a "complex" statement comprising one or more blocks of statements.
		// No terminating semi-colon needed after the closing brace to end the "complex" statement.
		// Take note that a "complex" statement is usually written over a few lines for readability.
			if (mark >= 50) {
			   System.out.println("PASS");
			   System.out.println("Well Done!");
			   System.out.println("Keep it Up!");
			}
		 
			if (number == 88) {
			   System.out.println("Got it!"); 
			} else {
			   System.out.println("Try Again!"); 
			}
		 
			i = 1;
			while (i < 8) {
			   System.out.print(i + " ");
			   ++i; 
			}
		 
			public static void main(String[] args) {
			   ...statements... 
			}

White Spaces:



Blank, tab and newline are collectively called white spaces. Java, like most of the computing languages, ignores extra white spaces. That is, multiple contiguous white spaces are treated as a single white space. You need to use a white space to separate two keywords or tokens to avoid ambiguity,
Example

int sum=0;       // Cannot write intsum=0. Need at least one white space between "int" and "sum"
double average;  // Again, need at least a white space between "double" and "average"
Additional white spaces and extra lines are, however, ignored, e.g.,
// same as above
int  sum 
 =  0      ;

   double  average  ;

Formatting Source Codes:



As mentioned, extra white spaces are ignored and have no computational significance. However, proper indentation (with tabs and blanks) and extra empty lines greatly improves the readability of the program. This is extremely important for others (and yourself three days later) to understand your programs.


Example :-
//the following one-line hello-world program works. But can you read and understand the program?
public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}

Braces:

Java's convention is to place the beginning brace at the end of the line, and align the ending brace with the start of the statement.


Indentation:

Indent each level of the body of a block by an extra 3 (or 4) spaces.

 
/*
 * Recommended Java programming style
 */
public class ClassName {      // Place the beginning brace at the end of the current line
   public static void main(String[] args) {  // Indent the body by an extra 3 or 4 spaces for each level
   
      // Use empty line liberally to improve readability
      // Sequential statements
      statement;
      statement;
   
      // Conditional statement
      if (test) {
         statements;
      } else {
         statements;
      }
      
      // loop
      init;
      while (test) {
         statements;
         update;
      }
   }
}   // ending brace aligned with the start of the statement


For method overloading and Overriding please referer OOP concept




Continue/ Next Page



Click here to access OOp's Concept

feel free to ask any of your doubts