Java Variables

Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

  • String – stores text, such as “Hello”. String values are surrounded by double quotes
  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • float – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
  • boolean – stores values with two states: true or false

Continue reading Java Variables

Java Comments

Java Comments

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.


Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by Java (will not be executed). Continue reading Java Comments

Java Output Numbers

Print Numbers

You can also use the println() method to print numbers.

However, unlike text, we don’t put numbers inside double quotes:

Example

System.out.println(3);
System.out.println(358);
System.out.println(50000);

 

Continue reading Java Output Numbers

Java Output / Print

Print Text

You learned from the previous chapter that you can use the println() method to output values or print text in Java:

Example

System.out.println("Hello World!");

 

Continue reading Java Output / Print

Java Syntax

Java Syntax

In the previous chapter, we created a Java file called Main.java, and we used the following code to print “Hello World” to the screen:

Main.java

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

}

}

 

Continue reading Java Syntax

Java Getting Started

Java Install

Some PCs might have Java already installed.

To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command Prompt (cmd.exe):

C:\Users\Your Name>java -version

If Java is installed, you will see something like this (depending on version):

java version "22.0.0" 2024-08-21 LTS
Java(TM) SE Runtime Environment 22.9 (build 22.0.0+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 22.9 (build 22.0.0+13-LTS, mixed mode)

If you do not have Java installed on your computer, you can download it for free at oracle.com. Continue reading Java Getting Started

Java Introduction

What is Java?

Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

  • Mobile applications (specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and application servers
  • Games
  • Database connection
  • And much, much more!

Continue reading Java Introduction