


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































Course Description/Outline In-depth study of Java. Development of large efficient and reusable systems: encapsulation, templates, references, constructors and distructors, operator overloading, memory management, exception handling , std templates, library, programming by contracts, configuration management, documentation and testing. Java Programming Language Notes
Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used for:
public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }
The main() method is required and you will see it in every Java program: public static void main (String[] args)
Inside the main() method, we can use the println() method to print a line of text to the screen: public static void main(String[] args) {
However, you can add the final keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "final" or "constant", which means unchangeable and read-only): final int myNum = 15 ; myNum = 20 ; // will generate an error: cannot assign a value to a final variable nt myNum = 5 ; float myFloatNum = 5.99f; char myLetter = 'D'; boolean myBool = true; String myText = "Hello";
The println() method is often used to display variables. To combine both text and a variable, use the + character: String name = "John"; System.out.println("Hello " + name);
All Java variables must be identified with unique names. These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). The general rules for constructing names for variables (unique identifiers) are:
Data types are divided into two groups:
A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in Java:
A boolean data type is declared with the boolean keyword and can only take the values true or false: boolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c': char myGrade = 'B'; System.out.println(myGrade);
The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes: String greeting = "Hello World"; System.out.println(greeting);
Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are:
Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting:
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d": double myNum = 19.99d; System.out.println(myNum);
A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length() method: String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; System.out.println("The length of the txt string is: " + txt.length()); There are many string methods available, for example toUpperCase() and toLowerCase(): String txt = "Hello World"; System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD" System.out.println(txt.toLowerCase()); // Outputs "hello world"
The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace): String txt = "Please locate where 'locate' occurs!"; System.out.println(txt.indexOf("locate")); // Outputs 7
The + operator can be used between strings to combine them. This is called concatenation : String firstName = "John"; String lastName = "Doe"; System.out.println(firstName + " " + lastName); You can also use the concat() method to concatenate two strings: String firstName = "John "; String lastName = "Doe"; System.out.println(firstName.concat(lastName));
Because strings must be written within quotes, Java will misunderstand this string, and generate an error: String txt = "We are the so-called "Vikings" from the north."; The solution to avoid this problem, is to use the backslash escape character. The backslash () escape character turns special characters into string characters:
A Boolean expression is a Java expression that returns a Boolean value: true or false. You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true:
if ( condition ) { // block of code to be executed if the condition is true }
if ( condition ) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false }
if ( condition1 ) { // block of code to be executed if condition1 is true } else if ( condition2 ) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements
variable = (condition)? expressionTrue : expressionFalse;
Use the switch statement to select one of many code blocks to be executed.
switch( expression ) { case x: // code block break; case y: // code block break; default: // code block }
When Java reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block.
for (int i = 0 ; i < 5 ; i++) { System.out.println(i); }
There is also a " for-each " loop, which is used exclusively to loop through elements in an array :
for ( type variableName : arrayName ) { // code block to be executed } String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (String i : cars) { System.out.println(i);
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets : String[] cars;
To change the value of a specific element, refer to the index number:
cars[ 0 ] = "Opel";
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[ 0 ] = "Opel"; System.out.println(cars[ 0 ]); // Now outputs Opel instead of Volvo
To find out how many elements an array has, use the length property:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars.length); // Outputs 4
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0 ; i < cars.length; i++) { System.out.println(cars[i]); }
methods, such as System.out.println(), but you can also create your own methods to perform certain actions: public class Main { static void myMethod() { // code to be executed } }
Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:
public class Main { static void myMethod(String fname) { System.out.println(fname + " Kamau"); } public static void main(String[] args) { myMethod("Liam"); } } // Liam Kamau When a parameter is passed to the method, it is called an argument. So, from the example above: fname is a parameter , while Lilian is an argument.
You can have as many parameters as you like: public class Main { static void myMethod(String fname, int age) { System.out.println(fname + " is " + age); } public static void main(String[] args) { myMethod("John", 4 ); myMethod("Jenny", 8 ); myMethod("Auma", 21 ); }