Java is a programming language used by billions of devices. It is object-oriented, meaning that Java is based on objects which can do certain things.
In order to start you must download JDK- the Java Development Kit. You might have to set up some environment variables. Next, download Dr. Java, an IDE that will help you write and run programs.
After opening Dr. Java, click the new file button (top left). Then choose the path for your file, and for the name, write FirstJavaProgram.java.
Now in the file, write:
public class FirstJavaProgram {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Click compile and then click run. On the bottom console, it should print out Hello World!
The public static void main(String[] args) is the main method header. This is how all main methods run. Curly braces {} are used to mark the beginning and end of classes and methods.
Whenever you name a file, the class header must be the same. For example, since our file name was FirstJavaProgram.java our class is public class FirstJavaProgram.
System.out.println()
is how to print to the console. If you want to keep printing out to the same line use System.out.print()
. Quotes around the Hello, World! indicate that it is a String type, or a "string" of characters including letters and numbers. Semicolons are inserted at the end of the lines that don't end with curly braces. You can use // to comment out code. This tells the compiler to ignore these lines.
Variables are containers in Java that can store primitives or objects. A primitive is an integer (int), double (a number with a decimal point such as 10.1), or boolean (a value that is either true or false). There are many other types of primitives, but we won't have to worry about those for the AP exam. Primitives are not objects. We use an equal sign ( = ) to assign a value to a variable. When you create a variable, it called declaring it. When you set the variable to a value, it is referred to as initialization. When you create an object using the new keyword it is called instantiating it.
Variables can be declared in the following format:
//for primitives
variableType variableName = primitiveValue;
int a = 1;
double b = 1.0;
boolean c = true;
//for objects
//Method 1
objectType variableName;
variableName = new ObjectType(parametersGoHere);
Dog d;
d = new Dog(20);
//Method 2
objectType variableName = new ObjectType(parametersGoHere);
Dog d = new Dog(10);
Strings are another type of object but are declared like a primitive:
String x = "abc12";
Strings are just a collection of alphanumeric characters plus special characters surrounded by quotes. Because of strings, there is a difference between "1" and 1, as you can't do math with "1".
Parameters in Java are basically inputs that go in a method. They can be any type. They are declared by their type and a variable name.
If you don't want a parameter just type at the end().
Static methods are when you call className.method()
. These methods don't rely on a creation of an individual object.
//example method
public void doSomething(int a)//this tells the method to accept an integer value as the parameter
{
//some code
}
public void doSomething2() //no parameters here
{
}
Variable names must follow certain conventions:
Method Naming:
Class Naming:
Class Names
When you create a primitive and store it in the variable, that variable actually contains the value. However, for objects, things are a little bit different. An object variable only contains a reference, meaning that variable is just a pointer to the object.
int x = 10;
Dog d = new Dog();
The variable x stores the value 10. However the d variable has a reference to the Dog object.
Dog d = new Dog();
Dog d2 = d;
d = new Dog();
// d2 still points to the first dog
Whenever you set the variable to another instantiated variable, it just has a reference to the instantiated variable's reference at that instance. In this case, d2 still points to the first dog as at that time d was pointing to the first variable.
When you do 5 / 2 you get 2.5. Not the case in Java.
int x = 5;
int y = 2;
System.out.println(x/y);//prints out 2
What happened? Since both numbers were ints, the result in Java must be an int. Since 2.5 is not an int, it is a double, the .5 is truncated so the result is just 2. It won't round up - just cuts off everything after the decimal point.
However notice what happens when you change the first line:
double x = 5.0;
int y = 2;
System.out.println(x/y);//prints out 2.5
Since there is a double in the operation, Java will print out the .5 so the result would be 2.5.
However, after evaluating the following expression 1.0/1
returns 1.0, even though the result is equal to an integer.
Here are other ways to do other operations:
int a = 7 + 5;//a=12
int b = 6 -7; //b = -1
int x = 2/6; //x = 0 as 2/6 = .333 remove decimal 0.333-> 0
int c = 5 * 6;// c= 30
int d = 7 % 3; // d = 1