Basics
//
// Basics.java
//
// This program demonstrates how to declare variables, change
// variables, and print things.
public class Basics
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
// this is a comment, which is ignored by the compiler
/*
this is a multi-line comment,
which is also ignored by the compiler
*/
// declare an integer (int) variable n
int n = 5;
System.out.println(n);
// assign a new value to n
n = 7;
System.out.println(n);
// variables may be a basic type (e.g. int, float, double,
// boolean) or a reference type (e.g. String)
float x = 1.23f; // floating point
System.out.println(x);
double y = 1.23; // double precision float
System.out.println(y);
boolean isHappy = true; // boolean: true or false
System.out.println(isHappy);
// there are many conventions for naming variables;
// the most important thing is to be consistent
double my_number = 5.67; // snake case
System.out.println(my_number);
double myNumber = 5.67; // camel case
System.out.println(myNumber);
// helloWorld is a reference to a String object, and you
// can use this reference to call String functions
String helloWorld = "Hello, world!";
System.out.println(helloWorld);
System.out.println(helloWorld.substring(0, 4));
}
}
Output:
Hello, world!
5
7
1.23
1.23
true
5.67
5.67
Hello, world!
Hell