knowt ap exam guide logo

Variables and Expressions (Information via EIMACS)

a = 4;

is Java code meaning "Store the number 4 in the memory location represented by the variable a". It is a statement known as an assignment because it tells the computer to do something. The “=” is known as the assignment operator.


Integers

A variable can store many types of data. A data type declaration must be made before the variable is given value to specify which type.

For example, to store __int__egers (as above), this is as simple as the first line of the following statement:

int a; 
a = 4;
a = 6;

In Java programs, the value stored in the memory location represented by a variable is the one that is placed there by the last statement processed by the program that assigns a value to a. If the program in the code block above was run to completion, the variable a would have a value of 6.


Numerical Literals

A sequence of digits is called a numerical literal*. Java allows the use of Base 10 (decimal), Base 8 (octal), and Base 16 (hexadecimal) systems.*

Base 10 is what is usually thought of as a number, consisting of digits 0-9.

Base 8 starts with a 0 and uses the numbers 0-7.

Base 16 starts with 0x and uses the numbers 0-9 and then the letters a-f to correspond with 10-15 in order.

Most computer applications use Base 2 which is only 0-1. This creates binary.

Translating between Bases(Steps written using helpful online tips)

Translate Base 10 to Binary: Take the base 10 number to convert and divide by 2. Write down the remainder. Next, divide it by 2 again. Write down the remainder once more. Continue this process of dividing the quotient by 2 and writing down the remainder until the quotient becomes 0. Once you have a list of remainders, read them from bottom to top. This sequence of remainders is the binary equivalent of the original base 10 number.

Translate to Binary to Base 10: Take the binary number. Write down the place values of each digit, starting from the right and increasing by a power of 2 each time (1, 2, 4, 8, 16, etc.). Multiply each digit of the binary number by its corresponding place value. Add up the products to get the decimal equivalent.

Translate Binary to Base 8: Group the binary digits in groups of 3 starting at the right. Replace each group of 3 with the Base 10 equivalent.

Translate Binary to Base 16: Group the binary digits in groups of 4 starting at the right. Replace each group of 4 with the Base 10 equivalent.


Doubles

To store numbers that include a decimal point, we use the declaration double:

double a;
a = 12.5;

Sometimes, lossy conversions occur and remove some or all repeating decimals from the end of a number. Decimals are approximations.

Decimal numbers with more than 7 digits after the decimal point are printed by Java in scientific notation. The expression will be displayed just like a calculator displays scientific notation. Scientific notation can be entered in the same format as an input. An integer can be input as a double and will be returned without an error:

double a;
a = 3;

This returns the value 3.0.


Casting

The process of changing a value from one data type into another data type is called casting. For example, if we input a value of the type double and we want to convert it to type int, we could use the code below:

int a;
a = (int)12.5;

The value of a is 12.

The terminology to describe this situation is that “the value of a is cast to an int.” The vast majority of the time, casting from a double to an int simply truncates the number at the decimal point (or removes the digits after the decimal point without rounding).

Similarly, we can cast from a int to a double with the following code.

double a;
a = (double)5;

The value of a would be returned as 5.0 in this case.


Arithmetic Expressions

A sequence of variables and arithmetic operators (+, -, *, /, %) is known as an expression.

The value of the expression is returned as the same type that its components (operands) were declared in. These operands can be ints or doubles. Because of this, addition, subtraction, and multiplication work as expected. Division will return a value that does not include a remainder, as is displayed in the following code.

int a;
int b;
int c;

a = 7;
b = 5;
c = a / b;

The value of c is 1

For division, to get the remainder, you can use the operator %. This will return only the value of the remainder.

int a;
int b;
int c;

a = 7;
b = 5;
c = a % b;

The value of c is 2.

The symbol for subtraction can also be used as an operator in a situation with only one operand to return the negative version of a number. It essentially returns the value of 0-a

int a;
int b;
a = 7;
b = -a;

The value of b is -7.
int a;
int b;
a = -7;
b = -a;

The value of b is 7.

An arithmetic expression can also contain literals as operands, such as a + 3. Additionally, it can contain expressions as operands, such as in the case of 5 - 2 * 3. The typical algebraic rules of the order of operations apply in this case.

If a mix of doubles and integers are used, the value will take on the declaration for the expression. This is seamless if the declaration is a double (first code block), but if it needs to be an int, the expression must be cast to int first (second code block).

int a;
double b;
double c;

a = 5;
b = 3.2;
c = a + b;

The value of c is 8.2.
int a;
double b;
int c;

a = 5;
b = 3.2;
c = (int)(a + b);

The value of c is 8.

The arithmetic mean is the average of a collection of numbers.

The floor of a floating point number is the greatest integer less than or equal to the number being discussed. It is the value of a double that is cast to an int if the double is a positive number.

Rounding tells the nearest integer to a floating point number. It rounds up to the higher integer if it is exactly between two integers. As long as a is greater than -0.5, it can be calculated by setting an expression as (int)(a + 0.5).


Pitfalls and Surprises

The range of values acceptable as integers in Java are -2147483648 through 2147483647. If you add 1 to 2147483647, the resulting number will be -2147483648 because, to keep memory use low, the bits will begin “recycling” to the beginning of the range at this number rather than adding more bits. This range increases greatly when dealing with doubles. If an overflow occurs in a double expression, the answer “infinity” will be reported, rather than the numbers starting over.

These numbers will be displayed by Java without any warning or error displayed, so it’s important for programmers to notice when an overflow may happen.


Error Messages

All values used in an expression need to have been (1) declared, and (2) assigned a value.

If a variable cannot be found, a “cannot find symbol” error will occur.

If the variable itself can be found but its value cannot, the “variable might not have been initialized” error will occur. This variable may have been given a value, but the value needed to have been given before the expression was evaluated.


Programming Shortcuts

Declaring a variable and assigning a value to it can be done in a singular statement and line of code. For example,

double p = 3.1;

This declares the variable p a double and initializes it (or sets its value) to 3.1. This type of “shortcut” statement also can be applied for expressions.

When a variable in an expression has a value that it is modifying and replacing, it can use an arithmetic/assignment operator in the form +=, -=, *=, /=, or %=.

int a = 5;
a += 3;

The value of a is 8.

Incrementing a number is very common in Java as well. This is when 1 is added or subtracted to a number. This can take the shorthand form of

int j = 5
int k = 11

j++;
k--;

The value of j is 6 and the value of k is 10.

Strings

A segment of text containing letters, digits, and other symbols is a “string.” The Java identifier for a string is simply “String.” (Case-Sensitive. Must start with an uppercase S.)

Particular strings are referred to as “string literals”. They must be enclosed with double-quote marks (““).

String s = "Hello World";

Strings are made out of characters. Each character in a string has an associated number. This number is called its index. Starting at 0, it counts up from the beginning of the string.

To include quotation marks in the output, place a backslash before each quotation mark \”like this\”. Because of this functionality, the backslash is known as an escape character. Even a backslash can be included in the output if another backslash is put directly before it as an escape character.

Concatenation

A concatenation operator chains together two strings by putting a + between them. Number literals or strings are able to be used with strings of words when concatenating.

String a = "You owe me $" + 2.25;

This returns You owe me $2.25

The shortcut of += can be applied to concatenation. All previous rules apply.

String Methods

length()

To determine the length of a string, we can use the message length(). The message must be sent to a certain variable using any variable in place of a below.

String a = "abcde";
int g = a.length();

This returns 5

substring ()

A sequence of adjacent characters in a string is called a substring. Any substring is specified if we know the index of the substring’s first and last characters. We can send a message for the substring using code below.

String a = "abcde".substring(1,3);

This returns bc

The first number should be the index of the first character in the substring. The second number should be one more than the index of the last character in the substring.

If the substring is the tail of the string (meaning that it includes the final character of the string), the only index that must be specified is that of the first character.

indexOf()

Use the indexOf() message to determine whether a specific index is part of a string.

int n = "abcde".indexOf("cde");

This returns the value 2.

indexOf always returns a value in the form of an integer. If no matching substring is found, the value -1 is returned. Otherwise, the index of the first character in the string is returned.

Displaying Messages

Using the message System.out.println(), we are able to control what exactly is displayed as an output by the Java code.

int n = 5;
System.out.println("n has " + n + " as its value");

n has 5 as its value

Multiple System.out.println() messages will create a new line of output in order with their contents. A different message that is similar is System.out.print(), which writes its message directly after the previous output on the same line.

Converting Between Numbers and Strings

The statement Double.parseDouble() allows you to convert a string that names a floating point value to its floating point value.

The statement Integer.parseInt() allows you to convert a string that names an integer value to its integer value.

The statement Integer.toString() allows you to convert an integer value to a string of its name.

The statement Double.toString() allows you to convert a double value to a string of its name.

BM

Variables and Expressions (Information via EIMACS)

a = 4;

is Java code meaning "Store the number 4 in the memory location represented by the variable a". It is a statement known as an assignment because it tells the computer to do something. The “=” is known as the assignment operator.


Integers

A variable can store many types of data. A data type declaration must be made before the variable is given value to specify which type.

For example, to store __int__egers (as above), this is as simple as the first line of the following statement:

int a; 
a = 4;
a = 6;

In Java programs, the value stored in the memory location represented by a variable is the one that is placed there by the last statement processed by the program that assigns a value to a. If the program in the code block above was run to completion, the variable a would have a value of 6.


Numerical Literals

A sequence of digits is called a numerical literal*. Java allows the use of Base 10 (decimal), Base 8 (octal), and Base 16 (hexadecimal) systems.*

Base 10 is what is usually thought of as a number, consisting of digits 0-9.

Base 8 starts with a 0 and uses the numbers 0-7.

Base 16 starts with 0x and uses the numbers 0-9 and then the letters a-f to correspond with 10-15 in order.

Most computer applications use Base 2 which is only 0-1. This creates binary.

Translating between Bases(Steps written using helpful online tips)

Translate Base 10 to Binary: Take the base 10 number to convert and divide by 2. Write down the remainder. Next, divide it by 2 again. Write down the remainder once more. Continue this process of dividing the quotient by 2 and writing down the remainder until the quotient becomes 0. Once you have a list of remainders, read them from bottom to top. This sequence of remainders is the binary equivalent of the original base 10 number.

Translate to Binary to Base 10: Take the binary number. Write down the place values of each digit, starting from the right and increasing by a power of 2 each time (1, 2, 4, 8, 16, etc.). Multiply each digit of the binary number by its corresponding place value. Add up the products to get the decimal equivalent.

Translate Binary to Base 8: Group the binary digits in groups of 3 starting at the right. Replace each group of 3 with the Base 10 equivalent.

Translate Binary to Base 16: Group the binary digits in groups of 4 starting at the right. Replace each group of 4 with the Base 10 equivalent.


Doubles

To store numbers that include a decimal point, we use the declaration double:

double a;
a = 12.5;

Sometimes, lossy conversions occur and remove some or all repeating decimals from the end of a number. Decimals are approximations.

Decimal numbers with more than 7 digits after the decimal point are printed by Java in scientific notation. The expression will be displayed just like a calculator displays scientific notation. Scientific notation can be entered in the same format as an input. An integer can be input as a double and will be returned without an error:

double a;
a = 3;

This returns the value 3.0.


Casting

The process of changing a value from one data type into another data type is called casting. For example, if we input a value of the type double and we want to convert it to type int, we could use the code below:

int a;
a = (int)12.5;

The value of a is 12.

The terminology to describe this situation is that “the value of a is cast to an int.” The vast majority of the time, casting from a double to an int simply truncates the number at the decimal point (or removes the digits after the decimal point without rounding).

Similarly, we can cast from a int to a double with the following code.

double a;
a = (double)5;

The value of a would be returned as 5.0 in this case.


Arithmetic Expressions

A sequence of variables and arithmetic operators (+, -, *, /, %) is known as an expression.

The value of the expression is returned as the same type that its components (operands) were declared in. These operands can be ints or doubles. Because of this, addition, subtraction, and multiplication work as expected. Division will return a value that does not include a remainder, as is displayed in the following code.

int a;
int b;
int c;

a = 7;
b = 5;
c = a / b;

The value of c is 1

For division, to get the remainder, you can use the operator %. This will return only the value of the remainder.

int a;
int b;
int c;

a = 7;
b = 5;
c = a % b;

The value of c is 2.

The symbol for subtraction can also be used as an operator in a situation with only one operand to return the negative version of a number. It essentially returns the value of 0-a

int a;
int b;
a = 7;
b = -a;

The value of b is -7.
int a;
int b;
a = -7;
b = -a;

The value of b is 7.

An arithmetic expression can also contain literals as operands, such as a + 3. Additionally, it can contain expressions as operands, such as in the case of 5 - 2 * 3. The typical algebraic rules of the order of operations apply in this case.

If a mix of doubles and integers are used, the value will take on the declaration for the expression. This is seamless if the declaration is a double (first code block), but if it needs to be an int, the expression must be cast to int first (second code block).

int a;
double b;
double c;

a = 5;
b = 3.2;
c = a + b;

The value of c is 8.2.
int a;
double b;
int c;

a = 5;
b = 3.2;
c = (int)(a + b);

The value of c is 8.

The arithmetic mean is the average of a collection of numbers.

The floor of a floating point number is the greatest integer less than or equal to the number being discussed. It is the value of a double that is cast to an int if the double is a positive number.

Rounding tells the nearest integer to a floating point number. It rounds up to the higher integer if it is exactly between two integers. As long as a is greater than -0.5, it can be calculated by setting an expression as (int)(a + 0.5).


Pitfalls and Surprises

The range of values acceptable as integers in Java are -2147483648 through 2147483647. If you add 1 to 2147483647, the resulting number will be -2147483648 because, to keep memory use low, the bits will begin “recycling” to the beginning of the range at this number rather than adding more bits. This range increases greatly when dealing with doubles. If an overflow occurs in a double expression, the answer “infinity” will be reported, rather than the numbers starting over.

These numbers will be displayed by Java without any warning or error displayed, so it’s important for programmers to notice when an overflow may happen.


Error Messages

All values used in an expression need to have been (1) declared, and (2) assigned a value.

If a variable cannot be found, a “cannot find symbol” error will occur.

If the variable itself can be found but its value cannot, the “variable might not have been initialized” error will occur. This variable may have been given a value, but the value needed to have been given before the expression was evaluated.


Programming Shortcuts

Declaring a variable and assigning a value to it can be done in a singular statement and line of code. For example,

double p = 3.1;

This declares the variable p a double and initializes it (or sets its value) to 3.1. This type of “shortcut” statement also can be applied for expressions.

When a variable in an expression has a value that it is modifying and replacing, it can use an arithmetic/assignment operator in the form +=, -=, *=, /=, or %=.

int a = 5;
a += 3;

The value of a is 8.

Incrementing a number is very common in Java as well. This is when 1 is added or subtracted to a number. This can take the shorthand form of

int j = 5
int k = 11

j++;
k--;

The value of j is 6 and the value of k is 10.

Strings

A segment of text containing letters, digits, and other symbols is a “string.” The Java identifier for a string is simply “String.” (Case-Sensitive. Must start with an uppercase S.)

Particular strings are referred to as “string literals”. They must be enclosed with double-quote marks (““).

String s = "Hello World";

Strings are made out of characters. Each character in a string has an associated number. This number is called its index. Starting at 0, it counts up from the beginning of the string.

To include quotation marks in the output, place a backslash before each quotation mark \”like this\”. Because of this functionality, the backslash is known as an escape character. Even a backslash can be included in the output if another backslash is put directly before it as an escape character.

Concatenation

A concatenation operator chains together two strings by putting a + between them. Number literals or strings are able to be used with strings of words when concatenating.

String a = "You owe me $" + 2.25;

This returns You owe me $2.25

The shortcut of += can be applied to concatenation. All previous rules apply.

String Methods

length()

To determine the length of a string, we can use the message length(). The message must be sent to a certain variable using any variable in place of a below.

String a = "abcde";
int g = a.length();

This returns 5

substring ()

A sequence of adjacent characters in a string is called a substring. Any substring is specified if we know the index of the substring’s first and last characters. We can send a message for the substring using code below.

String a = "abcde".substring(1,3);

This returns bc

The first number should be the index of the first character in the substring. The second number should be one more than the index of the last character in the substring.

If the substring is the tail of the string (meaning that it includes the final character of the string), the only index that must be specified is that of the first character.

indexOf()

Use the indexOf() message to determine whether a specific index is part of a string.

int n = "abcde".indexOf("cde");

This returns the value 2.

indexOf always returns a value in the form of an integer. If no matching substring is found, the value -1 is returned. Otherwise, the index of the first character in the string is returned.

Displaying Messages

Using the message System.out.println(), we are able to control what exactly is displayed as an output by the Java code.

int n = 5;
System.out.println("n has " + n + " as its value");

n has 5 as its value

Multiple System.out.println() messages will create a new line of output in order with their contents. A different message that is similar is System.out.print(), which writes its message directly after the previous output on the same line.

Converting Between Numbers and Strings

The statement Double.parseDouble() allows you to convert a string that names a floating point value to its floating point value.

The statement Integer.parseInt() allows you to convert a string that names an integer value to its integer value.

The statement Integer.toString() allows you to convert an integer value to a string of its name.

The statement Double.toString() allows you to convert a double value to a string of its name.

robot