Java-Tutorial
Java-Tutorial
System.out.println("Hello World");
}}
Java Syntax
public class Main {
System.out.println("Hello World");
}}
Java Output
( Print Text )
System.out.println("Hello World!");
System.out.println("It is awesome!");
( Print Numbers )
System.out.println(3);
System.out.println(358);
System.out.println(50000);
Java Comment
// This is a comment
Java Variables
You can also use the + character to add a variable to another variable: Example
Example
String firstName = "John ";
System.out.println(fullName);
For numeric values, the + character works as a mathematical operator (notice that
we use int (integer) variables here):
int x = 5;
int y = 6;
Example
Instead of writing:
You can also assign the same value to multiple variables in one line:
Example
int x, y, z;
x = y = z = 50;System.out.println(x + y + z);
Java Identifiers
All Java variables must be identified with unique names. These unique names are
called identifiers. Identifiers can be short names (like x and y) or more descriptive
names (age, sum, totalVolume). Note: It is recommended to use descriptive names in
order to create understandable and maintainable code:
Example
// Goodint minutesPerHour = 60;
int m = 60;
Java DataTypes
As explained in the previous chapter, a variable in Java must be a specified data
types.
System.out.println(myNum);
Short
short myNum = 5000;
System.out.println(myNum);
Int
int myNum = 100000;
System.out.println(myNum);
Long
long myNum = 15000000000L;
System.out.println(myNum);
Float
float myNum = 5.75f;
System.out.println(myNum);
Double
double myNum = 19.99d;
System.out.println(myNum);
Boolean Types
Very often in programming, you will need a data type that can only have one of two
values, like:
For this, Java has a boolean data type, which can only take the values true or false:
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
Example
char myGrade = 'B';
System.out.println(myGrade);
Example
char myVar1 = 65, myVar2 = 66, myVar3 =
67;System.out.println(myVar1);System.out.println(myVar2);System.o
ut.println(myVar3);
Strings
The String data type is used to store a sequence of characters (text). String values
must be surrounded by double quotes:
Example
System.out.println(greeting);
Type casting is when you assign a value of one primitive data type to another type.
Widening Casting
int myInt = 9;
System.out.println(myInt); // Outputs 9
}}
Narrowing Casting
System.out.println(myInt); // Outputs 9
}}
Java Operators
Operators are used to perform operations on variables and values. In the example
below, we use the + operator to add together two values:
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Java Strings
Example
Get your own Java Server Create a variable of type String and assign it a value:
String Length
A String in Java is actually an object, which contain methods that can perform certain
operations on strings. For example, the length of a string can be found with the
length() method:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
There are many string methods available, for example toUpperCase() and
toLowerCase():
String txt = "Hello World";
The indexOf() method returns the index (the position) of the first occurrence of a
specified text in a string (including whitespace):
Example
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
String Concatenation
The + operator can be used between strings to combine them. This is called
concatenation:
Example
String firstName = "John";
WARNING!
Java uses the + operator for both addition and concatenation. Numbers are added.
Strings are concatenated. If you add two numbers, the result will be a number:
Example
int x = 10;
int y = 20;
Example
String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)