/**
* Copyright (C) 2024 by Martin Robillard.
* See https://codesample.info/about.html
*/
package essentials;
/**
* Java is a statically-typed language. This means that each variable must have
* a type explicitly specified in the source code. Java supports two different kinds
* of types: and .
*/
public class Types
{
public static void main(String[] args)
{
day = 25;
month = Integer.(3); // March
Integer year = 2024;
[] months = {"January", "February", "March", "April", "May"}; // ...
System.out.println(day + " " + months[month-1] + " " + year);
System.out.println(String.join(" ", Integer.toString(day),
months[month-1], Integer.toString(year)));
// Copying the value of a primitive type copies the primitive value
int dayCopy = day;
day++;
System.out.println("dayCopy=" + dayCopy);
// Copying the value of a reference type creates an alias to the same object
String[] monthsAlias = months;
months[0] = "JANUARY";
System.out.println("monthsAlias[0]=" + monthsAlias[0]);
}
}
"A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values [Java Tutorial]."
"A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values [Java Tutorial]."
A reference type is an array type or the type of an object.
A reference type is an array type or the type of an object.
The +
(string concatenation) operator automatically converts its operands to
String
values. In this case, variables day
and year
are converted to
String
s before being concatenated with month[2]
.
The +
(string concatenation) operator automatically converts its operands to
String
values. In this case, variables day
and year
are converted to
String
s before being concatenated with month[2]
.
Another way to concatenate strings is to use the String.join
method. This method is convenient when
a separator must be inserted between arguments. However, in contrast to the concatenation operator (+
),
the type of the arguments to a method are not automatically converted. For this reason, it
is necessary to call the toString
method to convert the values to strings.
Another way to concatenate strings is to use the String.join
method. This method is convenient when
a separator must be inserted between arguments. However, in contrast to the concatenation operator (+
),
the type of the arguments to a method are not automatically converted. For this reason, it
is necessary to call the toString
method to convert the values to strings.
Java can automatically convert between a primitive type and its wrapper type, and vice-versa.
This feature is called autoboxing/unboxing. In this case, the literal integer value 2024
is "autoboxed" into an instance of the wrapper type Integer
.
Arrays are reference types in Java.
Arrays are reference types in Java.
An important aspect of primitive types is that when a variable is assigned the value of another variable, the value is copied. Changing the value of the initial variable does not change the value of the copy. The same principle applies to values passed as arguments to methods.
An important aspect of primitive types is that when a variable is assigned the value of another variable, the value is copied. Changing the value of the initial variable does not change the value of the copy. The same principle applies to values passed as arguments to methods.
An important aspect of reference types is that when a variable is assigned the value of another variable, a reference to the original object is stored in the copy. In effect, this simply creates an alias of the initial variable. Changing the value of the object referenced in the initial variable is thus reflected in the alias since it is just a different name for the same object. The same principle applies to values passed as arguments to methods.
An important aspect of reference types is that when a variable is assigned the value of another variable, a reference to the original object is stored in the copy. In effect, this simply creates an alias of the initial variable. Changing the value of the object referenced in the initial variable is thus reflected in the alias since it is just a different name for the same object. The same principle applies to values passed as arguments to methods.
Type int
is one of the most commonly-used primitive types. It corresponds to a 32-bit signed integer value.
Type int
is one of the most commonly-used primitive types. It corresponds to a 32-bit signed integer value.
Primitive types have corresponding wrapper types. A wrapper for a primitive type is an object that stores the primitive value in a field.
Integer
class wraps a value of the primitive type int
in an object. An object of type Integer
contains a single field whose type is int
.
Primitive types have corresponding wrapper types. A wrapper for a primitive type is an object that stores the primitive value in a field.
Integer
class wraps a value of the primitive type int
in an object. An object of type Integer
contains a single field whose type is int
.
In addition, this class provides several methods for converting an int
to a String
and a String
to an int
, as well as other constants and methods useful when dealing with an int
.
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Implementation note: The implementations of the "bit twiddling" methods (such as highestOneBit
and numberOfTrailingZeros
) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
The proper way to obtain an instance of a wrapper type is to use the static valueOf
method
(in contrast to a constructor call). Such a method is called a static factory method.
Integer
instance representing the specified int
value. If a new Integer
instance is not required, this method should generally be used in preference to the constructor Integer(int)
, as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
The proper way to obtain an instance of a wrapper type is to use the static valueOf
method
(in contrast to a constructor call). Such a method is called a static factory method.
Integer
instance representing the specified int
value. If a new Integer
instance is not required, this method should generally be used in preference to the constructor Integer(int)
, as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
i
- an int
value.
Integer
instance representing i
.
Despite its pervasive use, String
is a reference library type and not a primitive type.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Despite its pervasive use, String
is a reference library type and not a primitive type.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
A String
represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
.
The String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator
class provides methods for finer-grain, locale-sensitive String comparison.
javac
compiler may implement the operator with StringBuffer
, StringBuilder
, or java.lang.invoke.StringConcatFactory
depending on the JDK version. The implementation of string conversion is typically through the method toString
, defined by Object
and inherited by all classes in Java.
Integer
class wraps a value of the primitive type int
in an object. An object of type Integer
contains a single field whose type is int
.
Integer
class wraps a value of the primitive type int
in an object. An object of type Integer
contains a single field whose type is int
.
In addition, this class provides several methods for converting an int
to a String
and a String
to an int
, as well as other constants and methods useful when dealing with an int
.
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Implementation note: The implementations of the "bit twiddling" methods (such as highestOneBit
and numberOfTrailingZeros
) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
A String
represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
.
The String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator
class provides methods for finer-grain, locale-sensitive String comparison.
javac
compiler may implement the operator with StringBuffer
, StringBuilder
, or java.lang.invoke.StringConcatFactory
depending on the JDK version. The implementation of string conversion is typically through the method toString
, defined by Object
and inherited by all classes in Java.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println
methods in class PrintStream
.
print(String)
and then println()
.
print(String)
and then println()
.
x
- The String
to be printed.
String
object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int)
method.
String
object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int)
method.
i
- an integer to be converted.
CharSequence elements
joined together with a copy of the specified delimiter
.
CharSequence elements
joined together with a copy of the specified delimiter
.
For example,Note that if an element is null, thenString message = String.join("-", "Java", "is", "cool"); // message returned is: "Java-is-cool"
"null"
is added.
delimiter
- the delimiter that separates each element
elements
- the elements to join together.
String
that is composed of the elements
separated by the delimiter
NullPointerException
- If delimiter
or elements
is null