Unit 4 - Java language construcs Flashcards
Goals: – what primitive data types Java has and what value range they cover. – how variables can be temporarily stored with values. – what important operators exist in Java and how they are used. – what control structures are available and how statements can be repeated for control purposes. – how the visibility of classes and their elements can be defined in targeted ways using packages.
What are the 4 primitive data types?
- logical value (boolean) - True/False
- integer (byte, short, int, long) - whole numbers
- floating point number (float, double) - any number
- Character (char) - letters & symbols
What is a string?
It has properties of a primitive data type but it is a java class.
It is used to save strings of any length. The class String offers many existing methods for processing strings.
What is a variable?
It stores values in the application memory. They are similar to attributes, but the can only be used in the method body.
What are arithmetic operators?
they can execute mathematical functions
- ++ (increment, increases a variable by the value 1)
- (arithmetic addition)
- (arithmetic subtraction)
- · (arithmetic multiplication)
- / (arithmetic division)
- % (Remainder/modulus, calculates the remainder of the arithmetic division)
What are logical operators?
These are operators used to run logical functions, they return a value of true or false and can only result in a boolean.
- ! (logical complement/NOT, changes the truth value of the operand)
- && (logical AND, returns true when both operators are true)
- || (logical OR, returns true if one of the two operands is true)
- ^ (exclusive OR, returns true if one operand is true and one operand is false)
What are relational operators?
They compare expressions against each other and return boolean as the result type.
- == (equal, returns true if the values of the operands are equal)
- != (non-equal, returns true if the values of the operands are not equal)
- < (Less than, returns true if the value of the operand one the left is less than the value on the right)
- <= (Less than or equal to, returns true if the the value on the left is less than or equal to the value on the right)
- > (Greater than, returns true if the value of the operand on the left is greater than the value of the operand on the right.)
- > = (Greater than or equal to, returns true if the value of the operand on the left is greater than or equal to the value of the operand on the right.)
- instanceof (type comparison, returns true if the data types of both operands match)
What is a concatenation of strings (operator)?
It is a function of the data type String that creates a new string from two existing strings through simply stringing them together.
- (joining strings, joining multiple strings to form a new string)
What are control structures?
They are elements of a programming language for conditional or nested execution of statements.
It enables controlled repeat execution of statements. The most important control structures in Java are:
- conditional branches (if-else)
- loops (for, while, do-while)
Explain conditional branches in more detail.
- if-else statement (checks a condition and tests where the program continues, either the if or the else code block is executed)
- expanded if-else statement (not just one condition is tested before the else block is processed, but multiple, mutually exclusive conditions)
Explain loops in more detail.
Loops allow sequential execution of the statements, the number of iterations of the loop is determined by the satisfaction of a loop condition.
- while loop (if loop condition is true, executes the statements in the loop, the condition is tested again etc.)
- do-while loop (statements in the loop are always executed at least once, after that the condition is tested)
- for loop (initialization, test the condition, evaluates condition, if true executes all statements, then executes the statement in the loop increment)
What is a package?
It is an element used for structuring the java classes of a development project. Java classes are assigned to packages. Packages can themselves also contain packages.
The first line of the source text in a Java class always contains the package in which the class is located. The class names must be unique within a package.
Explain the visibility modifier.
- element is visible to all classes in the program
~~~
public class Order {}
public voic calculateSum () {}
~~~ - element is only visibile in the same package
~~~
class Order {}
voic calculate Sum() {}
~~~ - element is only visible in the same package or derived classes
~~~
protected void calculateSum() {}
protected int quantityProduct;
~~~ - element is only visible for elements of the same class
~~~
private void calculateSum() {}
private int quantityProduct;
~~~