Intro to JAVA Part 1

In this essay, I have decided to write about my personal experience at school. Today was the first day I went back to college after dropping off for two semesters, the feeling of going back to college is really great especially if you are going to study something you love.

In this article, I will be covering what I learned in class, and in this semester I will be studying JAVA this entire semester from scratch and I would like to share this knowledge with you so sit back and enjoy reading and learning.

What is JAVA

Java allows you to play online games, talk with individuals across the globe, assess your mortgage interest, and view 3D photos, just to name a few. The basis of corporate computing is also an important part of intranet applications and other e-business solutions.

Java, used by some 9 million developers and running on 7 billion computers worldwide, is one of the most commonly used programming languages. In addition to that, the US military still uses JAVA as a programming language.

Run a JAVA Program

Simple Java Program

The first thing you will have to do is to create a class, in this case, I named my class HelloWorld and the name of the class should camel-🐪 -case. After you will have to open curly braces that will contain the code you would want to execute.

In the next step you will have to write the name method, just memorize line 1 and line 2 as it is. Let us see the definition of some of our code:

  • public static void main => The main() method is static so that JVM can invoke it without instantiating the class. … As main() method doesn’t return anything, its return type is void. The Java program also terminates as soon as the main() method ends.
  • []args => In Java args contains the supplied command-line arguments as an array of String objects. In other words, if you run your program as java MyProgram one-two, [“one”, “two”] will be stored in args. If the contents of args were to be output, you can only loop through them.
  • System.out.println = > is a statement which prints the argument passed to it. println() shows what you want to display on the DOM or the console.

Note:

You have to remember to your statement with a semicolon for your code to run otherwise it will give you an error.

In the image above whatever is in the orange color is called the keyword, and whatever in the green is basically what you are executing. After that, you just end your program with closing curly braces.

As a reminder, every program you write will follow the code structure above.

Comments In JAVA

  • Single-line Comments = > you could simply use // if your comment is short, programmers use Single-line Comments a lot to leave messages for other programmers when they look at their code in the work field for example. As you can see in the image below on line number 4.
  • Multi-line Comments => Multi-line Java comments are used wherever we need to explain a procedure or basically write a paragraph of comments, single-line comments become tedious in this case as we will need to write ‘//’ at the start of every line.
  • Documentation Comments => Large code for a programming package uses this kind of Java comments as it generates a reference documentation page that can be used to get data about techniques, their parameters, and others.

This is basically the purpose of writing comment because maybe you want to explain something to your supervisor or you work partners when they look at your code or maybe even for yourself to help you remember a certain process you wanted to follow to solve a task.

What I want to cover next is System.out.println() :

  • System.out.println() = > The println() will execute your code on the DOM or the Console and it will automatically run the line of code that comes after that in a separate line.
  • System.out.print() = > The print() will execute that line only and it will print the next line of code as one sentence, look at the right side of the image below.

Definition of some JAVA words:

  • JVM => stands for Java Virtual Machine. It is a program for executing Java programs. It is also known as the Java runtime environment.
  • Bytecode verifier = > A program that checks whether the bytecode is correct.
  • Class Loader = > It’s a program that loads the Java bytecode to the Java Virtual Machine dynamically.
  • java Command = > This term means to run a Java program from the command line.
  • Bytecode = > It is generated for execution by the Java compiler. It’s neutral for channels.
  • javac Command => This is for a Java program to be compiled from the command line.

Installation

In short, if you want to do anything with JAVA make sure you download the JDK.

The second thing you will need to download is the IntelliJ IDE, this is just an integrated environment and it is great because it is a tool that you will use to build java code plus you can build so many things if you read the IntelliJ IDE documentation.

Variables

Variables are used to describe values in the program that can be modified. Typically, variables are used to store data that your Java program needs to do its job. This can be any sort of information from texts, codes (e.g. country codes, currency codes, etc.) to numbers, multi-step calculation temporary results, etc.

Example:

The method main() includes the declaration of a single integer variable called the number in the code example below. The integer variable value is set to 70 first, and then 20 is added to the variable afterward.

public class MyVariable {

public static void main(String[] args) {

int number = 7;

number = number + 20;
}

}

int = > To store a number in java, you have lots of options. Whole numbers such as 8, 10, 12, etc, are stored using the int variable. (The int stands for integer.) Floating-point numbers like 8.4, 10.5, 12.8, etc, are stored using the double variable.

Java Variable Types

  • Local variables => The local variable is called a variable specified within a block or method or builder.
public class StudentDetails {public void StudentAge(){// local variable ageint age = 0;age = age + 5;System.out.println("Student age is : " + age);}
  • Parameters => a variable that is passed to a method when the method is called.
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}

public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
  • instance variables => It is a variable belonging to an object. Objects in non-static fields retain their internal state.
class Example
{
static int a; //static variable
int b; //instance variable
}
  • class variables => A variable that is part of a class. For all the objects that use it, a static field has the same meaning.
class Emp {// static variable salarypublic static double salary;public static String name = "Harsh";}

Conclusion