# data[].attributes.attachment_link.readable_text
Netlify is the modern development platform for Enterprises to realize the speed, agility and performance of a scalable, composable web architecture.

Netlify Composable Web Platform

Streamlined orchestration, simplified and unified workflows, and real-time updates across infrastructure, workflows, websites and teams - all supported by Enterprise-grade security, services, and a world-class partner ecosystem.
Explore platform

3 solutions 1 platform

Netlify Connect

Unify all content

Netlify Connect is a data unification layer that gives web teams the freedom to create world-class websites, online stores, and applications with data from any existing or new content source.

Netlify Core

Deploy faster

Netlify Core is a frontend cloud solution for developers to build and deploy future-proof digital solutions with modern, composable tooling that works with all modern frameworks.

Netlify Create

Click, edit, publish

Netlify Create gives digital teams the freedom to autonomously perform content and website updates faster than ever before with an intuitive visual editor.

We aspire to be the most player-focused gaming company in the world, and Netlify absolutely carries the same spirit of aspiring to be the most developer-focused technology company in the world... Ultimately, there was a sense of deep collaboration, understanding and meeting our business requirements, and providing extremely knowledgeable and highly professional solutions every single step along the way.

# data[].attributes.attachment_link.title
Scale & Ship Faster with a Composable Web Architecture | Netlify

# data[].attributes.attachment_link.description
Realize the speed, agility and performance of a scalable, composable web architecture with Netlify. Explore the composable web platform now!

# data[].attributes.body
Tailmap

# data[].attributes.attachment_link.description
For 2D games or others, I create this for my first game, but my game did not succeed, so I decided to put it to download for free. xD Good Luck!

# data[].attributes.body
Strings:
{"Ada", "OLA", "Ela", "Ewa", "ada"}
1. Calculate the average length of the word.
2. Check if the array contains given name.
3. Calculate how many times given name is present in the array.
4. Calculate how many times the array contains name starting with given letter.
5. Find the longest word in the array.
6. Find the first word (alphabetically).
7. Find the shortest word(!) in the array.
8. Construct an array with names starting with given letter.
9. Correct names in the array.
10. Check how many times given letter is present in the array.

# data[].attributes.body
Praca domowa:
1. Średnia kwadratowa
2. Iloczyn wszystkich wyrazów
3. Suma sześcianów

# data[].attributes.attachment_link.readable_text
package com.company;

public class Main {

    public static void main(String[] args) {


        //1. For a given array of numbers find:
        int[] numbers = {50, 70};

        //a. the sum of the elements
        int sum = 0;
        for (int i = 0; i < numbers.length; i++) {
            //sum = sum + numbers[i];
            sum += numbers[i];
        }
        System.out.println("suma = " + sum);

        // b. the average value (arithmetic mean)
        System.out.println("average = " + (double) sum / numbers.length);

        //c. the geometric mean
        int product = 1;
        for (int i = 0; i < numbers.length; i++) {
            product *= numbers[i];
        }
        double geometricMean = Math.pow(product, 1.0 / numbers.length);
        System.out.println("gm = " + geometricMean);

        //harmonic mean
        double denominator = 0;
        for (int i = 0; i < numbers.length; i++) {
            denominator +=  1.0 / numbers[i];
        }
        System.out.println("hm = " + numbers.length / denominator);

        //the max value
        int max = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (max < numbers[i]) {
                max = numbers[i];
            }
        }
        System.out.println("max = " + max);


    }
}

# data[].attributes.attachment_link.readable_text
//1
int x = 5;
if (x > 10) {
	System.out.println("A");
}
else {
	System.out.println("B");
}


//2
int x = 0;
if (x < 0 || x > 10) {
	System.out.println("C");
}
else if (x > 0 && x < 5) {
	System.out.println("D");
}
else {
	System.out.println("E");
}


//3
int x = 11;
if (x > 0) {
	System.out.println("F");
	if (x > 2) {
		System.out.println("G");
	}
	else {
		System.out.println("H");
	}
	System.out.println("J");
}
else if (x > 10) {
	System.out.println("K");
}
else {
	System.out.println("L");
}


//4
boolean isAlive = true;
if (isAlive) {
	System.out.println("M");
}
else {
	System.out.println("N");
}


//5
int age = 34;
boolean adult = age >= 18;
System.out.println(adult);
if (adult && age < 65) {
	System.out.println("adult");
}
else if (adult){
	System.out.println("senior");
}
else {
	System.out.println("young");
}


//6
int x = 8;
if (x % 2 == 0) {
	System.out.println("even");
}
else {
	System.out.println("odd");
}


//7
double price = 1003.45;
System.out.println(price > 10_000 ? "expensive" : "cheap");

# data[].attributes.body
1. For a given array of numbers find:
a. the sum of the elements
b. the average value (arithmetic mean)
c. the geometric mean
d. the standard deviation
e. the max value
f. the min value
g*. the mode (the value that appears the most)
2. For a given array of numbers and a number:
a. check if this number is present in the array or not (T/F)
b. calculate how many times
c. return index of first appearance
d. return indexes of all appearances

# data[].attributes.attachment_link.readable_text
In computer programming, an array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.

String[] array = new String[100];

The number of values in the Java array is fixed. That is, the above array can not store more than 100 elements.

How to declare an array in Java?

In Java, here is how we can declare an array.

dataType[] arrayName;

For example,

double[] data;

Here, data is an array that can hold values of type double.
But, how many elements can array this hold?
Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,

// declare an array double[] data; // allocate memory data = new Double[10];

Here, the array can store 10 elements. We can also say that the size or length of the array is 10.

In Java, we can declare and allocate memory of an array in one single statement. For example,

double[] data = new double[10];

How to Initialize Arrays in Java?

In Java, we can initialize arrays during declaration. For example,

//declare and initialize and array int[] age = {12, 4, 5, 2, 5};

Here, we have created an array named age and initialized it with the values inside the curly brackets.

Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).

In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,

// declare an array int[] age = new int[5]; // initialize array age[0] = 12; age[1] = 4; age[2] = 5; ..

Java Arrays initialization

Note:

Array indices always start from 0. That is, the first element of an array is at index 0.

If the size of an array is n, then the last element of the array will be at index n-1.

How to Access Elements of an Array in Java?

We can access the element of an array using the index number. Here is the syntax for accessing elements of an array,

// access array elements array[index]

Let's see an example of accessing array elements using index numbers.

Example: Access Array Elements

class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5, 2, 5}; // access each array elements System.out.println("Accessing Elements of Array:"); System.out.println("First Element: " + age[0]); System.out.println("Second Element: " + age[1]); System.out.println("Third Element: " + age[2]); System.out.println("Fourth Element: " + age[3]); System.out.println("Fifth Element: " + age[4]); } }
Output

Accessing Elements of Array: First Element: 12 Second Element: 4 Third Element: 5 Fourth Element: 2 Fifth Element: 5

In the above example, notice that we are using the index number to access each element of the array.

We can use loops to access all the elements of the array at once.

Looping Through Array Elements

In Java, we can also loop through each element of the array. For example,

Example: Using For Loop

class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array // using for loop System.out.println("Using for Loop:"); for(int i = 0; i < age.length; i++) { System.out.println(age[i]); } } }
Output

Using for Loop: 12 4 5

In the above example, we are using the for Loop in Java to iterate through each element of the array. Notice the expression inside the loop,

Here, we are using the length property of the array to get the size of the array.

We can also use the for-each loop to iterate through the elements of an array. For example,

Example: Using the for-each Loop

class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array // using for loop System.out.println("Using for-each Loop:"); for(int a : age) { System.out.println(a); } } }
Output

Using for-each Loop: 12 4 5

Example: Compute Sum and Average of Array Elements

class Main { public static void main(String[] args) { int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12}; int sum = 0; Double average; // access all elements using for each loop // add each element in sum for (int number: numbers) { sum += number; } // get the total number of elements int arrayLength = numbers.length; // calculate the average // convert the average from int to double average = ((double)sum / (double)arrayLength); System.out.println("Sum = " + sum); System.out.println("Average = " + average); } }

Output:

Sum = 36 Average = 3.6

In the above example, we have created an array of named numbers. We have used the for...each loop to access each element of the array.

Inside the loop, we are calculating the sum of each element. Notice the line,

int arrayLength = number.length;

Here, we are using the length attribute of the array to calculate the size of the array. We then calculate the average using:

average = ((double)sum / (double)arrayLength);

As you can see, we are converting the int value into double. This is called type casting in Java. To learn more about typecasting, visit Java Type Casting.

Multidimensional Arrays

Arrays we have mentioned till now are called one-dimensional arrays. However, we can declare multidimensional arrays in Java.

A multidimensional array is an array of arrays. That is, each element of a multidimensional array is an array itself. For example,

double[][] matrix = {{1.2, 4.3, 4.0}, {4.1, -1.1} };

Here, we have created a multidimensional array named matrix. It is a 2-dimensional array. To learn more, visit the Java multidimensional array.

Recommended Readings

# data[].attributes.attachment_link.description
In computer programming, an array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[100]; The number of values in the Java array is fixed.

# data[].attributes.attachment_link.readable_text
package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

    public static final int TILES = 7;
    public static final int TILE_SIZE = 60;
    public static final int TILE_STROKE = 5;
    public static final int WIDTH = TILES * (TILE_SIZE + TILE_STROKE);
    public static final int HEIGHT = TILES * (TILE_SIZE + TILE_STROKE);

    Stage stage;

    public void printGame() {
        GridPane root = new GridPane();

        for (int y = 0; y < TILES; y++) {
            for (int x = 0; x < TILES; x++) {
                Rectangle rectangle = new Rectangle(TILE_SIZE, TILE_SIZE, Color.LIGHTGREEN);
                rectangle.setStroke(Color.GREEN);
                rectangle.setStrokeWidth(TILE_STROKE);
                root.add(rectangle, x, y);
            }
        }

        Scene scene = new Scene(root, WIDTH, HEIGHT);
        stage.setScene(scene);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        stage = new Stage();
        stage.setTitle("gra");
        stage.show();

        printGame();
    }


    public static void main(String[] args) {
        launch(args);
    }
}



        /*GridPane root = new GridPane();

        Rectangle rectangle = new Rectangle(100, 100, Color.INDIANRED);
        rectangle.setStroke(new Color(1, 0.5, 0, 1));
        rectangle.setStrokeWidth(5);
        root.add(rectangle, 1, 2);

        Button button = new Button("hello");
        button.setOnAction(event -> {
            rectangle.setFill(Color.GREEN);
            rectangle.setWidth(rectangle.getWidth() * 1.1);
        });
        root.add(button, 0,0);*/

# data[].attributes.body
1. Java od podstaw - poniedziałek 15:30.
2. Java dla średnio-zaawansowanych - wtorek 15:30.
3. C++ od podstaw - piątek 14:45.
4. C++ kontynuacja - czwartek 15:45.
Aby brać udział w zajęciach C++ trzeba się skontaktować z Panem Głuski (email na drzwiach do sali 22).

# data[].attributes.attachment_link.readable_text
package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //Add numbers from 1 to 100 that are divided by 7. (ver. 1)
        int sum1 = 0;
        int i = 0;
        while (i <= 100) {
            if (i % 7 == 0) {
                sum1 = sum1 + i;
            }
            i = i + 1;
        }
        System.out.println(sum1);

        //Add numbers from 1 to 100 that are divided by 7. (ver. 2)
        int sum2 = 0;
        int j = 0;
        while (j <= 100) {
            sum2 = sum2 + j;
            j = j + 7;
        }
        System.out.println(sum2);


        //Take a number n from the user. Print n! (factorial)
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = 1;
        int factorial = 1;
        while (k <= n) {
            factorial *= 2;
            k++;
        }
        System.out.println("factorial = " + factorial);


        //4. Take a number m from the user. Print m-first elements of the Fibonacci sequence.
        Scanner scanner2 = new Scanner(System.in);
        int m = scanner2.nextInt();

        int p = 0;
        int first = 0;
        int second = 1;
        System.out.println(first);
        System.out.println(second);
        while (p < m - 2) {
            int sum = first + second;
            System.out.println(sum);
            first = second;
            second = sum;
            p++;
        }

    }
}

# data[].attributes.body
1. Add numbers from 1 to 100 inclusive.
2. Add even numbers from 1 to 100 inclusive.
3. Add numbers from 1 to 100 that are divided by 7.
4. Take a number n from the user. Print n! (factorial).
5. Take a number n from the user. Print n-first elements of the Fibonacci sequence.
6. Collect two numbers a and b from the user. Compute a^b.

# data[].attributes.attachment_link.readable_text
Loop is used in programming to repeat a specific block of code until certain condition is met (test expression is false).

Loops are what makes computers interesting machines. Imagine you need to print a sentence 50 times on your screen. Well, you can do it by using print statement 50 times (without using loops). How about you need to print a sentence one million times? You need to use loops.

It's just a simple example. You will learn to use while and do..while loop to write some interesting programs in this article.

Java while Loop

The syntax of while loop is:

while (testExpression) {
    // codes inside body of while loop
}

How while loop works?

The test expression inside parenthesis is a boolean expression.

If the test expression is evaluated to true,

statements inside the while loop are executed.

then, the test expression is evaluated again.

This process goes on until the test expression is evaluated to false.

If the test expression is evaluated to false,

while loop is terminated.

Flowchart of while Loop

Example 1: while Loop

// Program to print line 10 times class Loop { public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.println("Line " + i); ++i; } } }

When you run the program, the output will be:

Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10

Notice, ++i; statement inside the while loop. After 10 iterations, variable i will be 11. Then, the test expression i <= 10 is evaluated to false and while loop terminates.

Example 2: Java while Loop

// Program to find the sum of natural numbers from 1 to 100. class AssignmentOperator { public static void main(String[] args) { int sum = 0, i = 100; while (i != 0) { sum += i; // sum = sum + i; --i; } System.out.println("Sum = " + sum); } }

When you run the program, the output will be:

Here, the variable sum is initialized to 0 and i is initialized to 100. In each iteration of while loop, variable sum is assigned sum + i, and the value of i is decreased by 1 until i is equal to 0. For better visualization,

1st iteration: sum = 0+100 = 100, i = 99
2nd iteration: sum = 100+99 = 199, i = 98
3rd iteration: sum = 199+98 = 297, i = 97
... .. ...
... .. ...
99th iteration: sum = 5047+2 = 5049, i = 1
100th iteration: sum = 5049+1 = 5050, i = 0

To learn more about test expression and how it is evaluated, visit relational and logical operators.

Java do...while Loop

The do...while loop is similar to while loop with one key difference. The body of do...while loop is executed for once before the test expression is checked.

The syntax of do..while loop is:

do {
   // codes inside body of do while loop
} while (testExpression);

How do...while loop works?

The body of do...while loop is executed once (before checking the test expression). Only then, the test expression is checked.

If the test expression is evaluated to true, codes inside the body of the loop are executed, and the test expression is evaluated again. This process goes on until the test expression is evaluated to false.

When the test expression is false, the do..while loop terminates.

Flowchart of do...while Loop

Example 3: do...while Loop

The program below calculates the sum of numbers entered by the user until user enters 0.

To take input from the user, Scanner object is used. Visit Java Basic Input to learn more on how to take input from the user.

import java.util.Scanner; class Sum { public static void main(String[] args) { Double number, sum = 0.0; Scanner input = new Scanner(System.in); do { System.out.print("Enter a number: "); number = input.nextDouble(); sum += number; } while (number != 0.0); System.out.println("Sum = " + sum); } }

When you run the program, the output will be:

Enter a number: 2.5 Enter a number: 23.3 Enter a number: -4.2 Enter a number: 3.4 Enter a number: 0 Sum = 25.0

Infinite while Loop

If the test expression never evaluates to false, the body of while and do..while loop is executed infinite number of times (at least in theory). For example,

while (true) { // body of while loop }

Let's take another example,

 int i = 100; while (i == 100) { System.out.print("Hey!"); }

The infinite do...while loop works in similar way like while loop.

# data[].attributes.attachment_link.description
Loop is used in programming to repeat a specific block of code until certain condition is met (test expression is false). Loops are what makes computers interesting machines. Imagine you need to print a sentence 50 times on your screen. Well, you can do it by using print statement 50 times (without using loops).

# data[].attributes.body
1. Print a sign of a given number (whether it is positive, negative or zero).
2. Give a test grade (A, B, C, D or F) for a student with marks from 0 to 100. State the school boundaries.
3. Find max value from 3 given numbers.
4. Test whether 3 given numbers can be the sides of a triangle (do they respect the triangular inequality?). Check whether this is a right triangle or not. Compute its area and perimeter.
5. Find the next day for the given date (note a leap year).

# data[].attributes.attachment_link.readable_text
In computer programming, it's often desirable to execute a certain section of code based upon whether the specified condition is true or false (which is known only during the run time). For such cases, control flow statements are used.

Java if (if-then) Statement

In Java, the syntax of the if-then statement is:

 if (expression) { // statements }

Here expression is a boolean expression. A boolean expression returns either true or false.

if the expression is evaluated to true, statement(s) inside the body of if (statements inside parenthesis) are executed

if the expression is evaluated to false, statement(s) inside the body of if are skipped from execution

How if statement works?

Working of Java if statement

Example 1: Java if Statement

class IfStatement { public static void main(String[] args) { int number = 10; // checks if number is greater than 0 if (number > 0) { System.out.println("The number is positive."); } System.out.println("This statement is always executed."); } }

Output:

 The number is positive. This statement is always executed.

In the above example, we have a variable named number. Here, the test expression checks if the number is greater than 0 ( number > 0).

Since the number is greater than 0. So the test expression evaluates to true. Hence code inside the body of if is executed.

Now, change the value of the number to a negative integer. Let's say -5.

int number = -5;

If we run the above program with the new value of the number, the output will be:

This statement is always executed.

Here, the value of number is less than 0. So, the test expression number > 0 evaluates to false. Hence, the body of if is executed.

To learn more about test expression, visit Java Relational Operators and Java Logical Operators.

Note: We can also use strings as the boolean expression.

Example 2: Java if...else with String

class Main { public static void main(String[] args) { // create a string variable String language = "Java"; // if statement if(language == "Java") { System.out.println("This is best programming language."); } } }
Output

This is best programming language.

Here, we are comparing two strings in the if block.

Java if...else (if-then-else) Statement

The if statement executes a certain section of code if the test expression is evaluated to true. However, if the test expression is evaluated to false, it does nothing.

In this case, we can use an optional else block. Statements inside the body of else block are executed if the test expression is evaluated to false. This is known as the if-then-else statement in Java.

The syntax of the if-then-else statement is:

if (expression) { // codes } else { // some other code }

Here, our program will do one task (task inside if block) if the test expression is true and another task (task inside else block) if the test expression is false.

How if...else statement works?

Working of Java if-else statements

Example 2: Java if else Statement

class IfElse { public static void main(String[] args) { int number = 10; // checks if number is greater than 0 if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } System.out.println("This statement is always executed."); } }

Output:

The number is positive. This statement is always executed.

In the above example, we have a variable named number. Here, the test expression checks if the number is greater than 0 ( number > 0).

Since the value of the number is 10, the test expression evaluates to true. Hence code inside the body of if is executed.

Now, change the value of the number to a negative integer. Let's say -5.

int number = -5;

If we run the program with the new value of the number, the output will be:

The number is not positive. This statement is always executed.

Here, the value of number is -5. So the test expression evaluates to false. Hence code inside the body of else is executed.

Java if..else..if Statement

In Java, we have an if...else...if ladder, that can be used to execute one block of code among multiple other blocks.

if (expression1) { // codes } else if(expression2) { // codes } else if (expression3) { // codes } . . else { // codes }

Here, if statements are executed from the top towards the bottom. As soon as the test expression is true, codes inside the body of that the if statement is executed. Then, the control of the program jumps outside the if-else-if ladder.

If all test expressions are false, codes inside the body of else is executed.

Example 3: Java if..else..if Statement

class Ladder { public static void main(String[] args) { int number = 0; // checks if number is greater than 0 if (number > 0) { System.out.println("The number is positive."); } // checks if number is less than 0 else if (number < 0) { System.out.println("The number is negative."); } else { System.out.println("The number is 0."); } } }

Output:

The number is 0.

In the above example, we are checking whether the number is positive, negative or zero. Here, we have two test expressions:

Here, the value of number is 0. So both the test expression evaluates to false. Hence the statement inside the body of else is executed.

Java Nested if..else Statement

In Java, it is also possible to if..else statements inside a if..else statement. It's called nested if...else statement.

Here's a program to find largest of 3 numbers:

Example 4: Nested if...else Statement

class Number { public static void main(String[] args) { // declaring double type variables Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber; // checks if n1 is greater than or equal to n2 if (n1 >= n2) { // if...else statement inside the if block // checks if n1 is greater than or equal to n3 if (n1 >= n3) { largestNumber = n1; } else { largestNumber = n3; } } else { // if..else statement inside else block // checks if n2 is greater than or equal to n3 if (n2 >= n3) { largestNumber = n2; } else { largestNumber = n3; } } System.out.println("The largest number is " + largestNumber); } }

Output:

The largest number is 4.5

Note: In the above programs, we have assigned the value of variables ourselves to make this easier. However, in real-world applications, these values may come from user input data, log files, form submission, etc.

Java provides a special operator called ternary operator, which is a kind of shorthand notation of if...else statement. To learn about the ternary operator, visit Java Ternary Operator.

# data[].attributes.attachment_link.description
In computer programming, it's often desirable to execute a certain section of code based upon whether the specified condition is true or false (which is known only during the run time). For such cases, control flow statements are used. In Java, the syntax of the if-then statement is: if (expression) { // statements } Here expression is a boolean expression.

# data[].attributes.attachment_link.readable_text
Java Variables

A variable is a location in memory (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Learn more about Java identifiers.

How to declare variables in Java?

Here's an example to declare a variable in Java.

int speedLimit = 80;

Here, speedLimit is a variable of int data type, and is assigned value 80. Meaning, the speedLimit variable can store integer values. You will learn about Java data types in detail later in the article.

In the example, we have assigned value to the variable during declaration. However, it's not mandatory. You can declare variables without assigning the value, and later you can store the value as you wish. For example,

int speedLimit;
speedLimit = 80;

The value of a variable can be changed in the program, hence the name 'variable'. For example,

int speedLimit = 80;
... .. ...
speedLimit = 90;

Java is a statically-typed language. It means that all variables must be declared before they can be used.

Also, you cannot change the data type of a variable in Java within the same scope. What is variable scope? Don't worry about it for now. For now, just remember you cannot do something like this.

int speedLimit = 80;
... .. ...
float speedLimit;

To learn more, visit: Can I change declaration type for a variable in Java?

Rules for Naming Variables in Java

Java programming language has its own set of rules and conventions for naming variables. Here's what you need to know:

Variables in Java are case-sensitive.

A variable's name is a sequence of Unicode letters and digits. It can begin with a letter, $ or _. However, it's convention to begin a variable name with a letter. Also, variable name cannot use whitespace in Java.

When creating variables, choose a name that makes sense. For example, score, number, level makes more sense than variable name such as s, n, and l.

If you choose one word variable name, use all lowercase letters. For example, it's better to use speed rather than SPEED, or sPEED.

If you choose variable name having more than one word, use all lowercase letters for the first word and capitalize the first letter of each subsequent word. For example, speedLimit.

There are 4 types of variables in Java programming language:

Instance Variables (Non-Static Fields)

Class Variables (Static Fields)

Local Variables

Parameters

You will learn about in later chapters. If you are interested to learn more about it now, visit Java Variable Types.

Java Primitive Data Types

As mentioned above, Java is a statically-typed language. This means that, all variables must be declared before they can be used.

int speed;

Here, speed is a variable, and the data type of the variable is int. The int data type determines that the speed variable can only contain integers.

In simple terms, a variable's data type determines the values a variable can store. There are 8 data types predefined in Java programming language, known as primitive data types.

In addition to primitive data types, there are also referenced data types in Java (you will learn about it in later chapters).

8 Primitive Data Types

boolean

The boolean data type has two possible values, either true or false.

Default value: false.

They are usually used for true/false conditions.

Example:

class BooleanExample { public static void main(String[] args) { boolean flag = true; System.out.println(flag); } }

When you run the program, the output will be:

byte

The byte data type can have values from -128 to 127 (8-bit signed two's complement integer).

It's used instead of int or other integer data types to save memory if it's certain that the value of a variable will be within [-128, 127].

Default value: 0

Example:

class ByteExample { public static void main(String[] args) { byte range; range = 124; System.out.println(range); // Error code below. Why? // range = 200 } }

When you run the program, the output will be:

short

The short data type can have values from -32768 to 32767 (16-bit signed two's complement integer).

It's used instead of other integer data types to save memory if it's certain that the value of the variable will be within [-32768, 32767].

Default value: 0

Example:

class ShortExample { public static void main(String[] args) { short temperature; temperature = -200; System.out.println(temperature); } }

When you run the program, the output will be:

int

The int data type can have values from -2³¹ to 2³¹-1 (32-bit signed two's complement integer).

If you are using Java 8 or later, you can use unsigned 32-bit integer with minimum value of 0 and maximum value of 2³²-1. If you are interested in learning more about it, visit: How to use the unsigned integer in java 8?

Default value: 0

Example:

class IntExample { public static void main(String[] args) { int range = -4250000; System.out.println(range); } }

When you run the program, the output will be:

long

The long data type can have values from -2⁶³ to 2⁶³-1 (64-bit signed two's complement integer).

If you are using Java 8 or later, you can use unsigned 64-bit integer with minimum value of 0 and maximum value of 2⁶⁴-1.

Default value: 0

Example:

class LongExample { public static void main(String[] args) { long range = -42332200000L; System.out.println(range); } }

When you run the program, the output will be:

-42332200000

Notice, the use of L at the end of -42332200000. This represents that it's an integral literal of long type. You will learn about integral literals later in this article.

double

The double data type is a double-precision 64-bit floating point.

It should never be used for precise values such as currency.

Default value: 0.0 (0.0d)

Example:

class DoubleExample { public static void main(String[] args) { double number = -42.3; System.out.println(number); } }

When you run the program, the output will be:

float

class FloatExample { public static void main(String[] args) { float number = -42.3f; System.out.println(number); } }

When you run the program, the output will be:

Notice that, we have used -42.3f instead of -42.3in the above program. It's because -42.3 is a double literal. To tell compiler to treat -42.3 as float rather than double, you need to use f or F.

char

It's a 16-bit Unicode character.

The minimum value of char data type is '\u0000' (0). The maximum value of char data type is ''.

Default value: '\u0000'

Example:

class CharExample { public static void main(String[] args) { char letter = 'Q'; System.out.println(letter); } }

When you run the program, the output will be:

You get the output Q because the Unicode value of Q is 'Q'.

Here is another example:

class CharExample { public static void main(String[] args) { char letter1 = '9'; System.out.println(letter1); char letter2 = 65; System.out.println(letter2); } }

When you run the program, the output will be:

When you print letter1, you will get 9 because letter1 is assigned character '9'.

When you print letter2, you get A because the ASCII value of 'A' is 65. It's because java compiler treats character as integral type. Learn more about ASCII.

Java also provides support for character strings via java.lang.String class. Here's how you can create a String object in Java:

myString = "Programming is awesome";

Java String is an important topic which you will learn in detail in later chapters. However, if you are not a newbie in programming and want to learn it now, visit Java String.

Java literals

To understand literals, let's take an example to assign value to a variable.

boolean flag = false;

Here,

boolean - is data type.

flag - is variable

false - is literal.

A Literal is the source code representation of a fixed value.

Values like 1.5, 4, true, 'P' that appears directly in a program without requiring computation are literals.

In the above example, flag is a variable. Since, it's a boolean type variable, it may store either false or true. For compiler to understand it, it requires computation. However, literals like -5, 'a', true represents fixed value.

Integer Literals

Integer literals are used to initialize variables of integer data types byte, short, int and long.

If an integer literal ends with l or L, it's of type long. Tip: it is better to use L instead of l.

// Error! literal 42332200000 of type int is out of range
long myVariable1 = 42332200000;

// 42332200000L is of type long, and it's not out of range
long myVariable2 = 42332200000L;

Integer literals can be expressed in decimal, hexadecimal and binary number systems.

The numbers starting with prefix 0x represents hexadecimal. Similarly, numbers starting with prefix 0b represents binary.

// decimal
int decNumber = 34;

// 0x represents hexadecimal
int hexNumber = 0x2F;

// 0b represents binary
int binNumber = 0b10010;

Floating-point Literals

Floating-point literals are used to initialize variables of data type float and double.

If a floating-point literal ends with f or F, it's of type float. Otherwise, it's of type double. A double type can optionally end with D or d. However, it's not necessary.

They can also be expressed in scientific notation using E or e.

 class DoubleExample { public static void main(String[] args) { double myDouble = 3.4; float myFloat = 3.4F; // 3.445*10^2 double myDoubleScientific = 3.445e2; System.out.println(myDouble); System.out.println(myFloat); System.out.println(myDoubleScientific); } }

When you run the program, the output will be:

Character and String Literals

They contain Unicode (UTF-16) characters.

For char literals, single quotation is used. For example, 'a', 'đ' etc.

For String literals, double quotation is used. For example, "programming", "Java 8"

Java also supports a few special escape sequences. For example, \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \" (double quote), \' (single quote), and \\ (backslash).

class DoubleExample { public static void main(String[] args) { char myChar = 'g'; char newLine = '\n'; String myString = "Java 8"; System.out.println(myChar); System.out.println(newLine); System.out.println(myString); } }

When you run the program, the output will be:

# data[].attributes.attachment_link.title
Java Variables and (Primitive) Data Types

# data[].attributes.attachment_link.description
A variable is a location in memory (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Learn more about Java identifiers. Here's an example to declare a variable in Java. int speedLimit = 80; Here, speedLimit is a variable of int data type, and is assigned value 80.

# data[].attributes.attachment_link.title
Intro to Java Programming - Course for Absolute Beginners

# data[].attributes.attachment_link.readable_text
We've noticed that JavaScript is disabled in your web browser. Please enable JavaScript in order to take advantage of all the features of this website.
Download IntelliJ Idea Ultimate for your operating system: Windows, macOS, Linux.
Download IntelliJ Idea Community for your operating system: Windows, macOS, Linux.

# data[].attributes.attachment_link.title
Download IntelliJ IDEA: The Java IDE for Professional Developers by JetBrains

# data[].attributes.attachment_link.description
We've noticed that JavaScript is disabled in your web browser. Please enable JavaScript in order to take advantage of all the features of this website. Download IntelliJ Idea Ultimate for your operating system: Windows, macOS, Linux. Download IntelliJ Idea Community for your operating system: Windows, macOS, Linux.

# data[].attributes.attachment_link.readable_text
This page is an archive of previously released builds of the JDK licensed under the GNU General Public License, version 2, with Classpath Exception.

WARNING: These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest security patches and are not recommended for use in production.

Releases

Notes

International use restrictions

Due to limited intellectual property protection and enforcement in certain countries, the JDK source code may only be distributed to an authorized list of countries. You will not be able to access the source code if you are downloading from a country that is not on this list. We are continuously reviewing this list for addition of other countries.

# data[].attributes.attachment_link.description
This page is an archive of previously released builds of the JDK licensed under the GNU General Public License, version 2, with Classpath Exception. WARNING: These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest security patches and are not recommended for use in production.