Introduction

Here is a Hello World code:

1
2
3
4
5
6
// FirstClass.java
public class FirstClass {
    public static void main(String[] args) {
        System.out.println("shesl-meow");
    }
}

Compile with the following command:

1
2
$ javac FirstClass.java
# Generate FirstClass.class in the same folder

Run the java program with the following command:

1
$ java FirstClass

Data Type

  1. The first data type we will use is int.
    1. int is short for integer, which are all positive and negative numbers, including zero. This number could represent the number of visits a website has received or the number of programming languages you know.
    2. The int data type only allows values between -2,147,483,648 and 2,147,483,647.
  2. The next data type we will use is the boolean.
    1. A boolean is a data type that can only be either true or false.
  3. The char data type is used to represent single characters. That includes the keys on a keyboard that are used to produce text.
    1. char is short for character and can represent a single character.
    2. All char values must be enclosed in single quotes, like this: 'G'.

Variable

We can assign a variable to a specified data type, like this:

1
int myLuckyNumber = 7;

The variable myLuckyNumber now stores the value 7 as an int type.

A semicolon ; is also used to end all Java single code statements. We will cover statements that should not end in a semicolon later in this course.

WhiteSpace

Whitespace is one or more characters (such as a space, tab, enter, or return) that do not produce a visible mark or text. Whitespace is often used to make code visually presentable.

Java will ignore whitespace in code, but it is important to know how to use whitespace to structure code well. If you use whitespace correctly, code will be easier for you and other programmers to read and understand.

Comments

A comment is text you want Java to ignore. Comments allow you to describe code or keep notes.

By using comments in the Java code, you may help yourself and even other programmers understand the purpose of code that a comment refers to.

In Java, there are two styles of comments: single line commentsand multi-line comments.

  1. Single line comments are one line comments that begin with two forward slashes:

    1
    
    // I'm a single line comment!
    
  2. Multi-line comments are generally longer comments that can span multiple lines. They begin with /* and end with */ . Here’s an example:

    1
    2
    3
    4
    5
    6
    
    /*
    
    Hello, 
    Java! 
    
    */
    

Math Operation

+, -, *, /, %

Relation Operation

Relational operators will always return a boolean value of trueor false.

Here are a few relational operators:

  1. < : less than.
  2. <=: less than or equal to.
  3. >: greater than.
  4. >=: greater than or equal to.

Equality Operator

The equality operators are:

  1. ==: equal to.
  2. !=: not equal to.

Equality operators do not require that operands share the same ordering. For example, you can test equality across boolean, char, or int data types. The example below combines assigning variables and using an equality operator:

1
2
3
char myChar = 'A';
int myInt = -2;
System.out.println(myChar == myInt);

The example above will print out false because the value of myChar ('A') is not the same value as myInt ('-2').