Java Fundamentals Flashcards
Is Java a compiled or interpreted language?
Java can be considered both a compiled and an interpreted language because its source code is first compiled into a binary byte-code. This byte-code runs on the Java Virtual Machine (JVM), which is usually a software-based interpreter.
What is the JDK?
The Java Development Kit (JDK) is one of three core technology packages used in Java programming, along with the JVM (Java Virtual Machine) and the JRE (Java Runtime Environment). It’s important to differentiate between these three technologies, as well as understanding how they’re connected:
What is the JVM
Java Virtual Machine
The JVM is the Java platform component that executes programs.
What is the Java Runtime Environment
The JRE is the on-disk part of Java that creates the JVM.
What is the JDK
The Java Development Kit:
The JDK allows developers to create Java programs that can be executed and run by the JVM and JRE.
What primitive types does Java provide?
Byte, short, int, long, float, double, Boolean and char
What is a byte?
byte: The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable’s range is limited can serve as a form of documentation.
What is a short?
short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
What is an int?
int: By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Use the Integer class to use int data type as an unsigned integer. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
What is a long?
long: The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2^64-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
What is a float?
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
What is a double?
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
What is a Boolean?
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined.
What is a char?
char: The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
What are some of the ways that a reference type differs from a primitive type?
Assignment
copies the contents of RHS variable into LHS variable
primitives: the primitive value is copied
references: the address is copied
implications: for references the object is not copied, it is shared (reference variables are aliases)
Comparisons (e.g. ==)
compares the contents of the variables
primitives: the primitive values are compared
references: the addresses are compared
implications: for references the contents of the objects are not compared
Passing Parameters
terminology:
formal parameter: the parameter variable that is listed (along with its type) in the method declaration
actual parameter: the parameter that is given when the method is called
copies the contents of actual parameter into the formal parameter (i.e., pass-by-value)
primitives: the primitive value is copied
references: the address is copied
implications: for references the object is not copied, it is shared (i.e., actual parameter and formal parameter are aliases)
primitives: changing the formal parameter’s value doesn’t affect the actual parameter’s value
references: changing the formal parameter’s address doesn’t affect the actual parameter’s address but changing the formal parameter’s object does change the actual parameter’s object since they refer to the same object
Returning Values
returns a result to where the method was called
primitives: the primitive value is returned
references: the address is returned
recall: local variables and parameters are destroyed when the method finishes execution
implications: a locally created object can survive if it is returned or if it is stored in a data member