Notes 1 - Introduction to Java(Brief History, Syntax, Variables, Comments)

 Objectives:


Brief History of Java

    Java was founded by James Gosling together with his colleagues Mike Sheridan and Patrick Naughton in June 1991. Back then, Java was initially called "Oak" after an oak tree that stood outside Gosling's office then it was called "Green" then finally called "Java" from Java coffee, a type of coffee from Indonesia. That is why the logo of Java look like this. 

Java Logo

    Gosling design Java with a C/C++ style syntax that system and application programmers would find familiar.

    As of today, Java is owned by Oracle and more than 3 billion devices run Java. It is used for:

  • Mobile Applications(specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and applications servers
  • Games
  • Database connection
  • And much, much more!


Syntax

    Syntax is defined structure or the design of a programming language and its functions. It serves as guide for programmers and developers in building a program. In Java, its syntax could look difficult and confusing but it is not. Just take a look at this very first Java program:

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

Example explained:

    Every line of code that runs Java must be inside a "class". In our example, we named the class "Hello". A class should always start with an uppercase first letter. 

NOTE: Java is case-sensitive: Hello and hello are not the same.

    When saving the file, save it using the class name and add ".java" at the end of the filename. In our example, it will be "Hello.java". And to actually run the program for the first time, you need to check if Java was properly installed, you can check the previous lesson. If you're using Visual Studio Code, you can go to "Terminal-New Terminal" to open the terminal window because it is on the terminal window where we are going to run our Java programs. On the terminal window, type "java" plus the filename together with the ".java". For our example, it will be "java Hello.java" and the output should look like this: 

PS C:\Users\Admin\Desktop\Java Files> java Hello.java Hello, world!


The main Method:

    The main() method is required in every Java program.

public static void main(String[] args)

    Any code inside main() method will be executed. Don't worry about the keywords before and after main because you'll get to know them along the way.

System.out.println()

    Inside the main() method, we can use println() to print something on the terminal window. In our example above, we wrote Hello, world! inside the println() and when we ran the program Hello, world! printed on the terminal window. Don't worry about Systemout and println(). You just need to put them together separated by period.

NOTE: Any text you want to print must be inside the double quotation marks "Hello, world". However, you can print numbers(whole number or with decimal) and true or false without double quotation marks.

    You can also calculate numbers inside println(). For example:

System.out.println(5+5);

    This gives us the output:

10

    Remember, if you want to calculate something inside println(), there should be no double quotation marks and letters. 5+5 and "5+5" means different. "5+5" means to literally print 5+5 on the terminal window while 5+5 means to calculate 5+5 which gives us an output 10. Every text, numbers and special characters inside double quotation marks are called String and we'll going to explore it in the next chapters. 

NOTE: The curly braces '{}' marks the beginning and the end of a block of code.


Comments

    Comments is used to say something within the java file without affecting the whole program. It can be helpful by marking a certain part of the code. Comments can be done in single-line or multiple lines

Single-line comments

    Single-line comments starts with //. Any text in a line after // will be ignored by Java(will not be executed)

//This is a comment
System.out.println("Hello, world!");

    Multi-line comments starts with /* and ends with */. Any text in between /* and */ will be ignored by Java(will not be executed).

/*
These are some text that Java will ignore
The code below will print the words Hello, world!
This is amazing! Isn't it?
*/
System.out.println("Hello, world!");

Single-line comments or Multi-line comments?

    It really depends on you. Most of the time // used for short comments and /* */ used for comments that could take few lines.


Variables

    Since we know that we can print something on the screen using println() that if its a text it must be inside two double quotation marks and if you want to calculate numbers just don't put them inside two double quotation marks. Now I am going to introduce variables. We use variables to store some data or information to it and use it when needed. You can think of variables as a storage box where we put some stuffs in it and when we need to use those stuffs we just have to get that box to be able to get what's inside that box. Same goes with variables. Variables looks like this:

String name = "Andrew";

Example explained:

    String specifies what is the type of the data, name is the name of the variable, "Andrew" is the value. In Java, there are different types of data:

  • String
  • int
  • float
  • char
  • boolean
  • class
  • interface

Declaring(Creating) Variables

type variableName = value;

    'type' refers to data type, 'variableName' refers to the name of the variable(such as name, x, etc) and equal sign '=' is used to assign values to variable.

    To create variable that should store text, look at the following example:

String name = "Andrew";
System.out.println(name);

    Here we call the variable "name" of type "String" and assign "Andrew" to it. Notice that we put the variable name inside println() which means to print Andrew on the terminal window. Note: If you're going to print the value of a variable, variable name must match, name and Name are not the same. There should be no double quotation marks for variables because if you're going put the name of the variable in between 2 double quotation marks, it will literally print the name of the variable and not the assigned value to it. 

    We can also create a variable that should store numbers: 

int age = 19;
System.out.println(age);

    int is one of the data types that stores number but not decimals, there are also other data types that stores non-decimal values. Note: int is not the same as Int. To create a variable that should store numbers with decimals, float or double must be used.

    We can also declare a variable without assigning value and assign the value later:

int age;
age = 19;
System.out.println(age);

    We can also overwrite the value of an existing variable:

int age = 19;
age = 25; // age is now 25
System.out.println(age);

Final variables

    We can also declare variables which cannot be overwrite or changed and that is by adding the keyword final before the type

final int age = 19;
age = 25; // Generates Error
System.out.println(age);

Other Types

int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";


Notes 2


Comments

Popular Posts