/**
* Copyright (C) 2024 by Martin Robillard.
* See https://codesample.info/about.html
*/
package essentials;
/**
* Represents an employee in a company.
*/
Employee
{
name;
String title ; // Default value
department;
}
/**
* Represents a department in a company.
*/
class Department
{
String name;
Department(String name)
{
.name = name;
}
String getName()
{
return "Department of " .name;
}
}
/**
* This class represents "client code" and it purpose is only to hold the main
* method.
*/
class ClassesAndObjects
{
public static void (String[] args)
{
// Create a new Employee object with the default constructor
Employee = ;
/*
* Accessing fields directly is not a recommended practice. We do it
* here only to illustrate the runtime structure of an object.
*/
= "Dilbert";
// Create a new Department object with an explicit constructor.
Department department = new Department("Great Features");
employee.department = department;
System.out.println(employee.name);
System.out.println(employee.title);
System.out.println();
System.out.println(employee.);
}
}
A class is a template for creating objects. A class declares fields (also called instance variables or, more rarely, attributes). Once into an object (also called instance), the object will have one instance variable for each field declared in the class. In Java, a class determines the final structure of an object: it is not possible to add or remove fields after an object is created.
A class is a template for creating objects. A class declares fields (also called instance variables or, more rarely, attributes). Once into an object (also called instance), the object will have one instance variable for each field declared in the class. In Java, a class determines the final structure of an object: it is not possible to add or remove fields after an object is created.
It is not mandatory to declare fields in a class. Classes also normally declare methods, and some situations call for classes that declare only methods.
It is not mandatory to declare fields in a class. Classes also normally declare methods, and some situations call for classes that declare only methods.
This code declares a getter (also called an accessor), because it returns the value of
a field. Getters often return the exact value of a field, but they can also perform other
operations, such as appending a suffix to a string. Getter methods are usually called get
followed
by the name of the field, or simply the name of the field (e.g., name()
).
This code declares a getter (also called an accessor), because it returns the value of
a field. Getters often return the exact value of a field, but they can also perform other
operations, such as appending a suffix to a string. Getter methods are usually called get
followed
by the name of the field, or simply the name of the field (e.g., name()
).
When at least one of the operands is a string, the +
operator concatenates (that is, joins) the two
operands into a single string. If one of the operands is not a string, it is first converted to a string.
There are multiple ways to concatenate strings in Java, and the +
operator is only one of them.
When at least one of the operands is a string, the +
operator concatenates (that is, joins) the two
operands into a single string. If one of the operands is not a string, it is first converted to a string.
There are multiple ways to concatenate strings in Java, and the +
operator is only one of them.
When there is no ambiguity, it is possible to leave out the this
keyword because the compiler
can infer whether an identifier refers to an instance variable of the current object. Whether to
include it or not is then a matter of coding style.
When there is no ambiguity, it is possible to leave out the this
keyword because the compiler
can infer whether an identifier refers to an instance variable of the current object. Whether to
include it or not is then a matter of coding style.
We use the new
operator to create an object. The keyword new
must be followed by a
constructor call.
Although class Employee
does not explicitly declare a constructor, it is still
possible to call a constructor with no parameters for this class. Under the covers,
Java implicitly creates a default constructor if none is explicitly declared.
A default constructor initializes each field using its initializer if there
is one, or the default value for its type if there is no initializer.
Although class Employee
does not explicitly declare a constructor, it is still
possible to call a constructor with no parameters for this class. Under the covers,
Java implicitly creates a default constructor if none is explicitly declared.
A default constructor initializes each field using its initializer if there
is one, or the default value for its type if there is no initializer.
The variable employee
contains a reference to the newly-created object
of class Employee
.
The variable employee
contains a reference to the newly-created object
of class Employee
.
Once an object is created, we can access its fields using the dot (.
) operator. The left
operand is the name of a variable of a reference type, and the right operand is
the name of the instance variable we wish to access. This representation illustrates that name
is an element of the object stored in the employee
variable, so we must access it through
the employee
variable.
Once an object is created, we can access its fields using the dot (.
) operator. The left
operand is the name of a variable of a reference type, and the right operand is
the name of the instance variable we wish to access. This representation illustrates that name
is an element of the object stored in the employee
variable, so we must access it through
the employee
variable.
This statement illustrates how the value of a field can be a reference to another object.
In this case, field department
of object employee
is assigned as value a reference
to the object stored in the local variable also named department
. Despite the reuse of the
name department
, there is no ambiguity because the use of the dot operator distinguishes
between the instance variable department
of an Employee
object and a local variable
named department
.
This statement illustrates how the value of a field can be a reference to another object.
In this case, field department
of object employee
is assigned as value a reference
to the object stored in the local variable also named department
. Despite the reuse of the
name department
, there is no ambiguity because the use of the dot operator distinguishes
between the instance variable department
of an Employee
object and a local variable
named department
.
This statement illustrates how objects can reference objects many level deep. Here an instance
of class Employee
contains a field department
of class Department
, which itself contains
an instance variable called name
.
This statement illustrates how objects can reference objects many level deep. Here an instance
of class Employee
contains a field department
of class Department
, which itself contains
an instance variable called name
.
The dot operator is also used to call methods on objects.
The dot operator is also used to call methods on objects.
Instantiation is the process of creating a new object of a given class.
Instantiation is the process of creating a new object of a given class.
This class contains three field declarations. A field declaration must include, at a minimum, the name of the field and its type. A field declaration can also include other elements, such as an initializer, or .
This class contains three field declarations. A field declaration must include, at a minimum, the name of the field and its type. A field declaration can also include other elements, such as an initializer, or .
An access modifier, such as public
, determines what parts of the code can access a field.
This concept is covered in detail in other code samples. If no access modifier is specified,
the field is only accessible by other code in the same package.
An access modifier, such as public
, determines what parts of the code can access a field.
This concept is covered in detail in other code samples. If no access modifier is specified,
the field is only accessible by other code in the same package.
A field declaration can include an initializer, which is an expression that assigns an initial (or default) value to the field when the object is created.
A field declaration can include an initializer, which is an expression that assigns an initial (or default) value to the field when the object is created.
Here Department
is a user-defined type, declared below. Declaring a class also
defines a new data type, which can then be used to declare the type of any variable,
including instance variables of other classes.
Here Department
is a user-defined type, declared below. Declaring a class also
defines a new data type, which can then be used to declare the type of any variable,
including instance variables of other classes.
This class defines a constructor. A constructor is a special function that initializes an object after it is created. A constructor is declared like a method, but its name is the name of the class, and there is no return type.
This class defines a constructor. A constructor is a special function that initializes an object after it is created. A constructor is declared like a method, but its name is the name of the class, and there is no return type.
The this
keyword, followed by a period, refers to the current object. Within a constructor,
it refers to the newly-created object. A common programming idiom when initializing a new
object with a constructor is to use the this
keyword to distinguish between the instance
variable (this.name
) and the formal parameter (name
). An obvious alternative is to use
a different name for the parameter (e.g., departmentName
, or pName
where p
stands for
"parameter").
The this
keyword, followed by a period, refers to the current object. Within a constructor,
it refers to the newly-created object. A common programming idiom when initializing a new
object with a constructor is to use the this
keyword to distinguish between the instance
variable (this.name
) and the formal parameter (name
). An obvious alternative is to use
a different name for the parameter (e.g., departmentName
, or pName
where p
stands for
"parameter").
This field is of type String
, a common library type.
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.
void
is a keyword used in place of a return type to indicate that a method does not return a value.
void
is a keyword used in place of a return type to indicate that a method does not return a value.
This is the only element that can change from the header declaration of a main
method: the name of the parameter can be any valid Java identifier.
This is the only element that can change from the header declaration of a main
method: the name of the parameter can be any valid Java identifier.
The main method must have a single parameter, which must be an array of String
. The values of this array are supplied when starting the program, typically through the command line. This type can also be expressed as a vararg (variable arguments), written as String...
.
The main method must have a single parameter, which must be an array of String
. The values of this array are supplied when starting the program, typically through the command line. This type can also be expressed as a vararg (variable arguments), written as String...
.
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.