Java Basics

Collapse Content

Java may be the most in-demand programming language, used in large companies, colleges and Android apps. Java is a compiled Object-oriented language designed to run quickly anywhere. This page covers the basics of Java. For more info, see Learn programming with Java.

Method Basics

The header of the method declares what the method will return (such as an int), the method name (doStuff), and the method's parameters (such as a and b). The method body follows, where you usually need to write code that returns a value at the end.

//method header: methodName(parameters)
static int doStuff(int a, int b){
  //your code here - erase this comment and write your code!
  return 1;
Syntax note: Every line of code in Java needs to end with a ; or it will be considered part of the next line. Comments are marked with 2 backslashes //, and are ignored by the compiler.

Variables and Data types

Variables need to be declared with their type before they can be used. You can declare and assign a variable on one line:

//type varName = value;
int someNum = 11;

Java has 8 different primitive data types, for holding different types of data:

int num = 5; // 32-bit integers can store values within the range of ± 2,147,483,647
boolean alive = true;  // true or false
double pi = 3.14159;  //  64-bit decimal numbers
char c = 'c';  //characters, which are stored internally as 16-bit integers

// Strings are objects, not primitives, but they can be declared and assigned like a primitive type:
String word = "hello";  

//less commonly-used types:
float f = 1.23f; // 32-bit decimal numbers
// bytes can store integers from -127 to 127. shorts can store 16-bit integers. 

See Variables and Methods , the basic Data types and More on Data types for more info.

Type Conversion and Casting
You can assign the value of smaller type to a larger type:

int i = 5;
double d = i;

This will print out:


When assigning a value to a different type, Java will automatically convert it, as long as they're compatible and the type being assigned (on the left) is larger than the type being copied (on the right).

If you assign a value to a smaller type, Java will raise an error, unless you explicitly cast it:

double di = 3.6;
long lo = 10L;
int i1 = (int) di;  // i1 = 3
int i2 =  (int) lo;  // i2 = 10

See more at Type Conversion and Casting.

Operations Booleans and Logic

Math operations are performed are performed according to the standard order of operations. Note that division of integers will round down to nearest integer. The mod % operator returns the remainder after division.

int product = 4 * (1+2) / 2;   //6.
int roughDivision = 8 / 3;   //2
int remainder = 3 % 2;     //1, the remainder of 3/2

booleans are used to store true/false values, such as the results of comparisons:

boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5);    //equals
check = (five != 3);   //not equals
check = (five >= 5);  //greater or equals

The logical operator AND && will return true if multiple conditions are all true, and OR || will return true if at least one condition is true.

//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR

See Logic and booleans for more.

if else and while

The if statement will execute code only if a condition is true. The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.

public static void controlFlow(int number) {
    //this if/else block will print out 1 statement for any number
    if(number > 10){
        System.out.println("big number");
    else if(number > 5){
        System.out.println("medium number");
        System.out.println("small number");
//this loop will print the numbers from 0 to 9 int i = 0; while(i<10){ System.out.println(i); i = i + 1; } }

See If Statement

Printing and Arrays

System.out.println will print text and add a newline afterwards. System.out.print will just print text, but you can add in spaces " ".

System.out.println("hello"); //prints with newline 
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world" 

Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets [].

//create an empty 5-cell array 
int[] ar1 = new int[5];
//create an array with numbers 0 to 4
int[] nums = {0,1,2,3,4};
//use for-loop to print values of array
for(int i=0; i < nums.length; i=i+1){
    System.out.print(nums[i]+" "); 

See Arrays and Loops for more.

Contact Us
Sign in or email us at [email protected]