What's java?

Java is a platform that can run on multiple operating systems such as Windows and macOS. You can write and run java programs on any of the supported operating systems interchangeably. As students learning to program it will make it much easier to support programming on whatever device you have. Of course, the device must be able to run the Java platform. As of June 1, 2019, the required software can be installed on Solaris, Windows, Linux, and macOS. 

Explain features of Java programming. – QCOM

Java Bytecode

There is a particular way to write Java programs. This allows the code to be translated into the proper bytecode. Bytecode is the file that is actually interpreted by the Java Virtual Machine. Converting into byte-code is called compiling the code.

Java Virtual Machine

Any computer that has a JVM installed will run java bytecode. JVM should interpret bytecode the same say whether or not you're coming from a Windows device to macOS devices. This allows you to share code across the supported different platforms. The JVM interprets the bytecode and executes it on your computer's processor.


The Java Runtime Environment contains the JVM and libraries of classes (a.k.a. a set of code) to run java programs.


The Java Development Kit includes both the JRE and JVM as well as other tools necessary to develop Java programs. If you are only interested in running Java programs you can install just the JRE. As you are interested in writing code and turning it into runnable bytecode for the JVM, you will need to install JDK if you want to mimic the setup your school may have on your personal machine. Here is a link to the installation instruction to help you set up your personal machine.

As mentioned in the overview, APCS-A only covers a subset of the language. You will slowly learn the syntax, the way of writing java programs. Remember your java program must be interpreted into byte-code. In order for this to happen, you must follow rules on what it means to be a properly constructed java program. Do not feel like you have to learn it all right away. A majority of the rules repeat as you continue to learn more complex programming concepts.

Naming a Java Source File

Every java program will be made up of java source files. Java source files have a file extension of .java similar to how Microsoft Word documents may have a file extension of .doc or .docx. The first line of the java program shown above helps determine what the file is named as a file. Every file in java is either a class, enum, or interface. In APCS-A, you will be writing classes. Line 1 in the program above after the keyword class determines the name of the file. This means that this program is stored in a java source file called HelloWorld.java.

Every class in java should be capitalized. Should the name of a class contain multiple words, it is best practice to capitalize the beginning of each word. The class HelloWorld has two words, hello and the world, therefore H and W are capitalized. This is often referred to as camel-casing. Anything else that is not a java class should start with lowercase and then follow camel-casing conventions.


In a given standalone java program, you will typically encounter what is known as the main method. The main method is the starting point for a java program. You do not need to know too much about what it means to be a java method for now. All you need to know is that the program will start inside of the main method. In the program below the first set of code that will execute will be anything after line 4.


Oftentimes, you will actually call upon existing code that has already been tested and proven to work. The line of code System.out.println("Hello World") is actually a call to a print method. Calling upon a method in Java involves calling the name of the method including the parenthesis. You may be familiar with using the square root function like sqrt(16). Parenthesis is a major part of a method call. The information you pass to a method goes inside the set and this input to the program is the method's parameters. In the sqrt method, 16 is the input to this method. The expected result would be the square root of that.

System Class

In order to call upon the print methods, you must always include System.out. This part of that line of code just says that the method you are accessing happens to live in a library called System. The very first set of codes you will call upon comes from the System class. This library of functions is included in Java.lang package and is automatically included. We will touch briefly on Java packages later.

Literal Values

Literals in java are how you represent actual data such as integers and strings. For example, the numbers 0, 1, and 3 are literal values. String literals in java are defined by double quotes as seen in the println method.

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

The println and print methods from the System class require text to print to the console. In this case, we provide it with a string literal by surrounding the desired text with double quotes. Below are all examples of string literals:

"Hello World" 
"Touch Steak"
"Such is life"
"To be or ! to be"

Variables and Types

There are two types of data in Java: primitives and objects. Objects are covered in the next chapter. Primitive types describe the type of data that can hold a specific value and has no method associated with it. The following are the main primitive data types covered on the AP Exam.

A variable in java is a name given to a space in memory. This allows you to store information at that location. In order for java to know how much room to make in memory, it needs to know what kind of data you're storing. As shown in the table, the different types can store a different range of values. Java is known as a strongly typed language. A strongly typed language is one that requires datatype to be determined and declared for variables. Other examples of strongly typed programming languages are C, C++, and Pascal.

4 bytes
Stores whole numbers from -2,147,483,648 to 2,147,483,647
double8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bit
Stores true or false values

Declaring Primitive Data Types in Java

The declaration of a variable in java requires 2 things, type, and variable name.
boolean my_bool; 
int my_int;
double my_double;
In the examples above, there are three variables named my_bool, my_int, and my_double. The variable my_bool can hold the values true or false. The int and double variables can hold any number in the range shown on the table. For example, my_int can hold the number 5, 8, or 9, and my_double can hold values like 1.51233, -5.4124, -3.14, etc

Valid Variable Names

Java reserved keywords like int, double, if, and while cannot be used as variable names. Variable names cannot begin with a number. It is best practice to use descriptive variable names. For example, a variable meant to hold age would best be called age instead of my_mom.

Now that you know how to prepare the storage space in memory in java, you should probably learn how to store actual literal values in the variables. Remember literal values in Java are the programming representation of a value. String literals, for example, are surrounded by quotation marks. For numerical types, you can simply use numbers as values.


int x; //declaration 
x = 1; //assignment
The two lines above are a declaration followed by an assignment. The variable name is x and it stores data of int type. The second line is the assignment statement. An assignment in java involves one equal ( = ) sign as shown.

Declaration and Assignment

You can declare variables and assign a value to them in the same line of code per the syntax below.
int my_var = 1; //declare and assign 

Below are more examples of assignment statements.

Code Segment 1:
int x = -1;

Code Segment 2 :
double my_double = 1.2;
my_double = 1.09;

Code Segment 3:

boolean a = true;
a = false;
a = true;


There are some shortcuts you can take when performing an assignment with a calculation. The following are the most common types you will encounter. They are known as compound assignments.

x = x + 1;x+=1;
x = x * b;x*=b;
x = x / t;x/=t;
x = x%y;x%=y;
x = x-z;x-=z;

Complex Compound Assignments

What happens if you mix compound statements with regular compound expressions?


Recall that numerical types in java have a range of values they can represent as shown in the table above. What happens when you go above or below that value? A simple way to show what happens is with the code segment below. Say you have a variable and you assigned it to have the maximum value of an int as shown in the table, what happens if you add to it?
int x = 2147483647;
What is printed? If you guessed 2147483648 then you guessed incorrectly. Remember that the ranges shown in the table are not suggestions, they are the absolute minimum and maximum values that each data type can hold. x+1 from the code segment actually results in -2147483648. This is known as an overflow. The number overflows and goes into the negatives or the minimum number the data type can represent. Think of it like the counter below. Once you pass 9 it goes back to 0! The same thing behavior is also trying for doubles and any other numerical type.


What happens if you have a numerical type and want to convert it to another numerical type? There are two types of casting that happen in java, implicit and explicit.

Implicit Casting

Implicit casting occurs when java automatically converts a calculation to use a type without you specifying. Implicit casting happens only when Java will decide to use a data type that can accommodate a bigger range of numbers. In other words, converting a smaller type to a larger type. In the APCS-A world, you need only worry about the casting from an int to a double. As shown on the table on the previous page, a double only hold decimal numbers but its range is also much bigger.

Below are examples of the Java language automatically casting to use a bigger type when working with doubles and ints.
System.out.println( 3/2.0);
The line above has a mixed-typed division. Whenever a double is part of an arithmetic operation between two numbers then the type that is used for the result is a double. The line above will print 1.5 to the console. This is part of the implicit casting mechanism in the Java programming language.

What happens if we changed it to the line below?
System.out.println( 3/2 );
The line above will now print 1 to the console! This is because mathematical operation between two integers always results in an integer.

Explicit Casting

Explicit casting is one where you specify to which data type you are converting. The syntax for this is through the use of parenthesis and the desired data type as shown below.
double var = 3.14; 
int int_var = (int) var; //cast to an int
The variable int_var is an int type, therefore, it cannot be set to be the value stored in the variable var. This means that you must specifically tell the computer that it is ok to lose some information about the number. In this particular case int_var now holds 3 instead of 3.14. This is one of the reasons you have to explicitly cast. Java will NOT do this for you automatically and results in an error. This is because Java knows that you are losing information if you try to store a decimal number in a container that cannot hold the decimal portion of the number. It is there to protect you at the end of the day in case you did not intentionally lose information.

You will see this a lot when you work with random numbers. This is also known as a loss of precision. Why? Because you losing the decimal portion of the number, you are left with only the data to the left of the decimal.