Java Programming I on YouTube:
https://www.youtube.com/playlist?list=PLLIqcoGpl73iaXAtS_-V_Xdx3mhTzPwb5
Two kinds of variables are distinguishable in Java:
Primitive type variables (like count)
Object reference variables (like str)
Primitive type variables directly store data in allocated memory space
Reference variables store the address of the object containing the data
An object is an instance of a class
The operator "new"
instantiates an object in memory
invokes object's class constructor
Constructor has the same name as its class
If no constructor method exists in a class, then Java compiler will supply a default constructor.
When default constructor creates an object, it initializes
each primitive field to zero
each object reference field to null.
Constructors cannot have a return type.
The constructor returns (transparently) the address of the newly created object, which is stored in the object reference variable
The reference variable holds
type of object in memory
memory address of the object
Method name and its parameter list formulate the method signature
Methods in a class with the same name are permitted as long as each method has a different parameter list. Such methods are known as overloaded methods
Overloaded methods are allowed for programmer's convenience.
Lots of classes and methods are already predefined by the time you start writing your own code:
some already written by other programmers in your team
many predefined packages, classes, and methods come from the Java Library.
Library: collection of packages
Package: contains several classes
class: contains several methods
Method: a set of instructions
To use a method, you must know:
Name of the class containing the method (like Math)
Name of the package containing the class (like java.lang)
Name of the method (like pow) and list of parameters
import java.lang.*; // imports package
Math.pow( 2, 3 ); // calls power method in class Math
Math.pow( x, y ); // another call
Like variables declared outside of the method parameter list, a method parameter can be
A primitive data type, or
An object
Passing primitives to a method differs from passing objects:
primitives are passed by copying the value (known as pass by value). The method, therefore, gets a copy of the original variable.
objects are passed by reference -- only memory address is passed -- no copying of the data takes place. The method, therefore, gets access to the original object.
String variables are reference variables:
String name; name = new String( "Freddy Krueger" ); name = "Michael Myers";
A String object is an instance of class String
The address of a String object with the value "Freddy Krueger" is stored in the name
String name = "Freddy Krueger";
String methods are called using the dot operator
Two oveloaded methods printf yield output on the standard output device:
System.out.printf( formatString ); System.out.printf( formatString, argumentList );
Here, formatString is a string specifying the format of the output, and argumentList is a list of arguments.
The argumentList is a list of arguments that consists of constant values, variables, or expressions
If there is one argument, the output is similar to System.out.print():
System.out.printf( "Hello there!" ); // uses only the format string
NOTE: String method format also uses syntax similar to printf
When printf has both format string and the argument list, the data is merged into formatted stream of tokens:
int centimeters = 15; System.out.printf( "There are %.2f inches in %d centimeters.%n", centimeters / 2.54, // multiple arguments on argument list are separated by commas centimeters );
Here, %.2f and %d are called format specifiers. The output is
There are 5.91 inches in 15 centimeters.
By default, there is a one-to-one correspondence between format specifiers and the arguments
The first format specifier %.2f is matched with the first argument, which is the expression centimeters/2.54
The second format specifier %d is matched with the second argument, which is centimeters
The format specifier %n moves the insertion point to the beginning of the next line
A format specifier for general, character, and numeric types has the following syntax:
%[argument_index$][flags][width][.precision]conversion
The expressions in square brackets are optional; they may or may not appear in a format specifier
%[argument_index$][flags][width][.precision]conversion
argument_index is an integer indicating position of the argument in the list. The first argument is referenced by "1$", the second by "2$", etc.
flags is a set of characters that modify the output format
width is a decimal integer indicating the minimum number of characters to be written to the output
precision is a decimal integer usually used to restrict (by rounding) the number of digits after decimal point
The required conversion is a character indicating how the argument should be formatted:
Integer, Float, and Double are classes designed to convert a numeric string into an internal numeric data type.
These classes are called wrapper classes for the corresponding primitive types.
A String consisting of only integers or decimal numbers is called a numeric string
To convert a string consisting of an integer to a value of the type int, we use the following expression:
Integer.parseInt(strExpression)
For example,
Integer.parseInt("6723") = 6723 Integer.parseInt("-823") = -823
parseInt is a method of the class Integer, which converts a numeric integer string into a value of the type int
To convert a String consisting of a decimal number to a value of the type float, we use the following expression:
Float.parseFloat(strExpression)
For example,
Float.parseFloat("34.56") = 34.56 Float.parseFloat("-542.97") = -542.97
parseFloat is a method of the class Float and is used to convert a numeric decimal string into an equivalent value of the type float
To convert a String consisting of a decimal number to a value of the type double, we use the following expression:
Double.parseDouble(strExpression)
For example,
Double.parseDouble("345.78") = 345.78 Double.parseDouble("-782.873") = -782.873
parseDouble is a method of the class Double, which is used to convert a numeric decimal string into an equivalent value of the type double