Here is a Hello World code:
| |
Compile with the following command:
| |
Run the java program with the following command:
| |
Data Type
- The first data type we will use is
int.intis 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.- The
intdata type only allows values between -2,147,483,648 and 2,147,483,647.
- The next data type we will use is the
boolean.- A
booleanis a data type that can only be eithertrueorfalse.
- A
- The
chardata type is used to represent single characters. That includes the keys on a keyboard that are used to produce text.charis short for character and can represent a single character.- All
charvalues must be enclosed in single quotes, like this:'G'.
Variable
We can assign a variable to a specified data type, like this:
| |
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.
Single line comments are one line comments that begin with two forward slashes:
1// I'm a single line comment!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:
<: less than.<=: less than or equal to.>: greater than.>=: greater than or equal to.
Equality Operator
The equality operators are:
==: equal to.!=: 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:
| |
The example above will print out false because the value of myChar ('A') is not the same value as myInt ('-2').