Logo

Learning Java

Start Date: 28-Mar-2020
End Date: 27-April-2020(expected)


Java Basics


What is Java ?

How to run a Java program ?

How this works?

When we run javac Hello.java, Java compiler compiles the code and convert it into bytecode. bytecode is something that can be understood by the machine irrespective of the coding platform. Hence, making Java platform independent.

Architecture

Hello World Project

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

Explanation

* In this code HelloWorld is the class name. Hence, file should be saved with name HelloWorld.java only.

It has two KeyWords viz., public and class. They have a specific meaning.

KeyWords: has a specific meaning in any programming language. Java programs are written by following specific rules using these keywords.

The public Java keyword is an access modifier. An access modifier allows us to define the scope or how other parts of our code or even someone else's code can access this code.

The class keyword is used to define a class with the name following the keyword. "HelloWorld" in this case and curly braces { } defines the class block.

Note: Keywords are case sensitive. PUBLIC and Public and even public are different things.

Methods: A collection of statements (one or more) that perform an operation. We'll be using a special method called the main method that Java looks for when running a program. It's the entry point of any Java code.

public is an access modifier. static is a keyword. We need to have this to find our method to run the code we are using here.

void is another keyword used to indicate that the method will not return anything more on that later.


Code Block: is used to define a block of code. It's mandatory to have one in a method declaration and it's here where we will be adding statements to perform certain tasks.

Statement: This is a complete command to be executed and can include one or more expressions. Here, below one is an statement:

System.out.println("Hello World");

What is JAR ?

JAR is an acronym for JAVA Archive. JAR = Java Archive

How to Create a .jar file from command line?

  1. Create .java file with class name. Say HelloWorld.java
  2. Compile the code: javac HelloWorld.java
  3. Create a new file with extension .mf. Say myfile.mf
  4. Inside this myfile.mf file write: Main-Class: HelloWorld (HelloWorld is the class name). Don't forget to add a new line.
  5. Now run the command jar -cvmf myfile.mf myjar.jar HelloWorld.class
  6. A file with name myjar.jar will be created.

This .jar file can be transferred and run form any platform

How to run a .jar file ?

  1. Go to .jar location.
  2. Type java -jar filename.jar
* Source| Official Documentation

overview


nutshell


What is OOP?

It is a method of programming where code is designed and based on the functions and attributes of the objects.

How did Programming Evolve?

programming evolution