Variable
Kinds of Variables
1 Instance Variables (Non-Static Fields)
Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static
keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed
of one bicycle is independent from the currentSpeed
of another.
2 Class Variables (Static Fields)
A class variable is any field declared with the static
modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static
since conceptually the same number of gears will apply to all instances. The code static int numGears = 6;
would create such a static field. Additionally, the keyword final
could be added to indicate that the number of gears will never change.
3 Local Variables
Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;
). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
4 Parameters
Recall that the signature for the main
method is public static void main(String[] args)
. Here, the args
variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) .
Naming Convention
Variable names are case-sensitive.
- Begin with a letter.
- Use full words instead of cryptic abbreviations.
- If the name consists of only one word, spell that word in all lowercase. If it consists of more than one word, capitalize the first letter of each subsequent word. If it stores a constant value, capitalizing every letter and separating subsequent words with the underscore character.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Primitive Data Types
Eight Primitive Data Types
- 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).
- 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).
- int: By default, the
int
data type is a 32-bit signed two's complement integer.- It has a minimum value ofand a maximum value of.
- 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 ofand a maximum value of . Use the Integer class to useint
data type as an unsigned integer. Static methods likecompareUnsigned
,divideUnsigned
etc have been added to theInteger
class to support the arithmetic operations for unsigned integers.
- long: The
long
data type is a 64-bit two's complement integer.- The signed long has a minimum value of and a maximum value of .
- 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 and a maximum value of . TheLong
class also contains methods likecompareUnsigned
,divideUnsigned
etc to support arithmetic operations for unsigned long.
- float: The
float
data type is a single-precision 32-bit IEEE 754 floating point.- Its range of values is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification.
- 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.
- double: The
double
data type is a double-precision 64-bit IEEE 754 floating point.- Its range of values 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.
- This data type should never be used for precise values, such as currency.
- boolean: The
boolean
data type has only two possible values:true
andfalse
.- This data type represents one bit of information, but its "size" isn't something that's precisely defined.
- 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).
Default Values
Fields
Data Type | Default Value (for fields) |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
String (or any object) | null |
boolean | false |
Local Variables
The compiler never assigns a default value to an uninitialized local variable. Accessing an uninitialized local variable will result in a compile-time error.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
String
The Java programming language provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String
object; for example, String s = "this is a string";
. String
objects are immutable, which means that once created, their values cannot be changed. The String
class is not technically a primitive data type.
String as a field of a class would have null
as default value.
caution
==
or equals
❓
Java uses String interning to store string literals.
It means:
- There is a pool, String literal pool, storing all String literals.
- When a new String literal was created, the JVM will look for this String literal from the String literal pool.
- If an equivalent String literal is found, the reference to the newly created String literal would be simply updated. (That's why
==
would returntrue
.) - If not, this new String literal would be added to the String literal pool and its reference would be returned.
- If an equivalent String literal is found, the reference to the newly created String literal would be simply updated. (That's why
new
What about If new
is used, the JVM is obliged to create a new String object. (That's why ==
would return false
.)
A deeper look
The output:
How many Strings
Conclusion
- Literal strings within the same class (§8) in the same package (§7) represent references to the same
String
object (§4.3.1). - Literal strings within different classes in the same package represent references to the same
String
object. - Literal strings within different classes in different packages likewise represent references to the same
String
object. - Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
- Strings computed by concatenation at run time are newly created and therefore distinct.
- The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
https://docs.oracle.com/javase/10/docs/api/java/lang/String.html#intern()
https://en.wikipedia.org/wiki/String_interning
https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5
Array
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Declaration
Creating
Initializing
or
Arrays Manipulations
The System
class has an arraycopy
method that you can use to efficiently copy data from one array into another:
Java SE provides several methods for performing array manipulations (common tasks, such as copying, sorting and searching arrays) in the java.util.Arrays
class.
The previous example can be modified to use the copyOfRange
method of the java.util.Arrays
class, as you can see in the ArrayCopyOfDemo
example. The difference is that using the copyOfRange
method does not require you to create the destination array before calling the method, because the destination array is returned by the method:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Constant Expressions
A compile-time constant expression is an expression denoting a value of primitive type or a String
that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type
String
(§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5) - Casts to primitive types and casts to type
String
(§15.16) - The unary operators
+
,-
,~
, and!
(but not++
or--
) (§15.15.3, §15.15.4, §15.15.5, §15.15.6) - The multiplicative operators
*
,/
, and%
(§15.17) - The additive operators
+
and-
(§15.18) - The shift operators
<<
,>>
, and>>>
(§15.19) - The relational operators
<
,<=
,>
, and>=
(but notinstanceof
) (§15.20) - The equality operators
==
and!=
(§15.21) - The bitwise and logical operators
&
,^
, and|
(§15.22) - The conditional-and operator
&&
and the conditional-or operator||
(§15.23, §15.24) - The ternary conditional operator
? :
(§15.25) - Parenthesized expressions (§15.8.5) whose contained expression is a constant expression.
- Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).
- Qualified names (§6.5.6.2) of the form TypeName
.
Identifier that refer to constant variables (§4.12.4).
Compile-time constant expressions of type String
are always "interned" so as to share unique instances, using the method String.intern
.
https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28