Home Week-1 Week-3

 

Java - Week 2

This is an introduction to Java programming language, this chapter is an essential resource that will provide you with the knowledge and tools you need to begin writing Java code. We'll start by writing simple pieces of code, to saving them on the system and finally executing the programs to perform the desired operation.

Let's start with the most basic Java program as an example

Basic JAVA Program

Java is a case-sensitive programming language, meaning that it treats uppercase and lowercase letters as distinct characters. Writing Main instead of main will not work. Also, the variable names "firstName" and "firstname" are considered to be different variables in Java.

In Java, the main method is used by default as the starting point of a program.

Modifiers

Modifiers play an important role in any Java program. A modifier in Java is a keyword that is used to change the behavior of a class, method, or field. Example visibility can be controlled using modifiers like public, private. Using the above program as example we can see two modifiers. Let us closely understand them

  1. public - this is an access modifier and it allows access to the class, method or field from anywhere across the program.

     

  2. static - allows us to use the method or field without creating an instance of the class.

    We will learn them in more detail in the upcoming chapters, so it's normal if you find any of the given examples confusing.

Return Type

In Java, return type refers to the data type of the value that is returned by the method. Let's look at a simple example to understand this.

In the given example, we can see that the function named square takes in a number which is of type integer and it returns the square of that number, which is an integer in itself. Therefore, we have specified int as the return type of the function.

Saving a JAVA Program

There are certain rules we need to follow in order to save a Java program before we can finally run it. The file name needs to be exactly same as the class contained in the file.

The above program needs to be saved in a file named myprogram.java for it to be ready for execution. Note that it is necessary for every file to have the extension .java

Running a JAVA Program

In order to run a Java Program, we first need to compile the saved program to bytecode before we can finally run it, which can be done using the command javac, for example

This creates a new file named myprogram.class which contains the bytecode that can be executed using the command java, like

Note that while running the bytecode we do not need to specify any extension like .java or .class

Primitive Data Types

Data types are a fundamental concept any programming language and refer to the type of values that can be stored within a specific variable.

A primitive data type in Java is a basic data type that is built into the Java language and is not an object created from a class. There are 8 primitive data types in java:

Declaring and Initializing Variables

A variable is used to store data in a Java program.

We can declare variables by specifying the type followed by the name of variable. Example if we want to initialize an integer with value 100, we can do it as follows:

or we can declare the variable before initialising it

Arithmetic Operators

Arithmetic operators are a set of mathematical operations used to perform arithmetic operations in Java programming language. They are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder) on numeric data types such as integers, floating-point numbers, etc. Arithmetic operators have the following symbols:

It's important to note that the type of the result of an arithmetic operation depends on the operand types. For example, when two integers are divided, the result is also an integer, but when a floating-point number is divided by an integer, the result is a floating-point number.

These operators can be used as following:

Type Casting

This is the process of converting variables from one data type to another, like an integer to float. There are two types of casting, implicit and explicit.

  1. Implicit conversion is when a data is automatically converted to a different type. Below is a program which is an example of that.
  1. Explicit conversion is when a data is converted to a different type by the programmer using the cast operator.

You may wonder why do we need to explicitly convert double to integer, but in the other case when we converted integer to double it was not required. The reason for this is that, Java implicitly casts data when we move from lower order to higher order since all the information is preserved.

Example, when we converted integer 100 to float 100.0, no information was lost about the integer. But on the other side, when we converted double 9.99 into integer 9, the precision after the point was lost. So, Java needs the programmer to explicitly specify that they intend to perform this type of cast where they may end up losing information.

Strings

In Java, a String is a sequence of characters that represents text. They can be declared and initialized as follows

Strings are represented as objects, which means they have methods that can be used to manipulate their contents. For example, you can concatenate two strings together using the "+" operator, or find the length of a string using the "length()" method.

Strings in Java are also immutable, which means that once a string is created, its value cannot be changed. Instead, operations that appear to modify a string actually create a new string with the modified value.

Some helpful methods that can be used on string objects -

Arrays

An array in Java is a data structure that allows you to store a fixed-size sequential collection of elements of the same data type. It can be thought of as a container that holds a group of items of the same type, such as integers or strings.

You can access each element in the array by its index, which is a numeric value that represents its position in the array. The first element in the array has an index of 0, the second has an index of 1, and so on.

Conditional Execution

Conditional statements allow the program to make decisions based on conditions. They allow a program to execute a certain section of code only if a specified condition is true. There are two ways by which we can do this:

if-else

switch-case

The switch statement is used to perform different actions based on different conditions. It evaluates the expression inside the switch statement and then matches the value of that expression against a set of possible case values. If the value matches any of the case values, the corresponding code block is executed.

We have to use break in every case because if there is no break statement, then once a case is met all the cases after that are executed unless a break statement is found. Let's see this through an example

Now, notice that there are no break statements inside any of these cases. So, here the output of this program is

break statement is optional in the default block, since anyway that's the last code block and there's nothing which will get executed after it.

Conditional Loops

Conditional loops in Java are control structures that allow the program to execute a certain block of code repeatedly, as long as a certain condition is met. We have 3 types of conditional loops in Java, let us see them one by one in detail.

while

A while loop in Java is a control flow statement that allows a block of code to be executed repeatedly while a particular condition is true.

The condition is evaluated at the beginning of each iteration of the loop. If the condition is true, the code inside the loop is executed. After the code is executed, the condition is evaluated again, and the process continues until the condition becomes false.

Here's an example of a while loop that prints the string "Print this line..." 10 times:

The output of the above loop is

The program iterates over the values of i from 0 to 9, inclusive, and outputs a single line for each iteration. Once i reaches 10, the loop terminates as the condition in the parentheses is no longer satisfied.

do-while

A do-while loop in Java is similar to a while loop, but with one key difference: the code inside the loop is guaranteed to be executed at least once, regardless of whether the condition is true or false.

The code inside the loop is executed once, and then the condition is evaluated. If the condition is true, the loop will execute again, and the process will continue until the condition becomes false. If the condition is false from the beginning, the loop will still execute once.

Let us understand the do while loop using a simple example

The output of the above loop is

The condition is initially not met, however, it is evaluated after the first iteration of the loop has been executed. As a result, the statement within the do block will be executed once.

for

A for loop in Java is a control flow statement that allows a block of code to be executed repeatedly for a fixed number of times, based on a specified condition.

Syntax of for loop is as follows:

The initialization statement is executed once at the beginning of the loop, and is typically used to initialize a loop counter variable. The condition is evaluated at the beginning of each iteration of the loop. If the condition is true, the code inside the loop is executed. After the code is executed, the update statement is executed, and the condition is evaluated again. The loop continues to execute as long as the condition is true.

Let us look at it through a simple example

The output of the above code will be

The program iterates over the values of i from 0 to 9, inclusive, and outputs a single line for each iteration. Once i reaches 10, the loop terminates as the condition is no longer satisfied.

Class

In Java, a class is a blueprint or template for creating objects. It defines the variables and methods that an object of that class will have.

You can think of a class as a complex type let's say Fruits, each fruit will have a certain properties like it's weight and amount of calories, it contains. Also, we can do something with each fruit, like eat it.

In order to represent all properties and methods to manipulate objects of a complex type like Fruits, we can define a class.

Defining a class

To declare a class in Java, you use the "class" keyword followed by the name of the class.

Each object of class Fruits will have two properties, weight and calories. Also eat() method can be invoked on every object of the class.

Object

An object, is an instance of a class and is used to access the variables and methods defined by the class. Each object of class Fruits will have a local copies of the variables weight and calories, therefore changing the weight of one object will not affect others.

Creating an object

In order to create objects, the type of variable is the class for which we want to create that particular object. Objects are created using the new operator, followed by a call to the constructor of the class. For example

Like if we want to create an object of Fruits class we can write the following code

Manipulating objects

We can define public mutator methods inside a class, which can be used by the objects of that class to modify themselves.

Note: this keyword is used to reference the current object

Example, this is a mutator method which can be defined inside Fruits class in order to modify variables of an object.

Reading variables

Public accessor methods can be defined inside a class in order to read values of the variables inside the objects of that class. Example, in order to read weight and calories of objects of Fruits class, we can define the following methods inside the class.

Constructors

These are special functions which have the same name as the class without any return type and are called at the time of creation of an object. We can create constructors in order to set up certain properties of an object at the time of creation of that object. Constructor for Fruits class:

By default, a class includes a non parameterized constructor when no explicit constructor is defined. However, when a constructor is explicitly defined, the default empty constructor is no longer automatically generated.

Overloading

Method overloading in Java allows a class to have multiple methods with the same name, but different parameters. Example

Copy constructor

A copy constructor in Java is a constructor that creates a new instance of an object from an existing one. It takes an object of the same class as input and then copies the properties of that object to the newly created instance.

Note: If the instance variables of an object are object themselves then we may end up aliasing instead of copying the variable. This results in these variables sharing the same memory location across different objects, and any modifications made to the variables in one object will also be reflected in all other objects.

Deep Copying vs Shallow Copying

Shallow copying refers to creating a new object and then copying the reference to the original object's memory address into the new object. This means that the new object points to the same memory location as the original object, and any changes made to the new object will also affect the original object.

Deep copying, on the other hand, creates a new object and then copies all of the original object's attributes and values to the new object. This means that the new object is completely independent of the original object, and any changes made to the new object will not affect the original object.

Let's see this with the help of an example

The weight of person1 also gets changed, even though we changed it only for person2. The reason for this change is that since favouriteFruit instance variable in the class Person is an object of type Fruits, when we copy it from person1 to person2using the = symbol, it shares the same memory location because it's a shallow copy.

Any changes made to the favouriteFruit of person2 will also affect the favouriteFruit of person1 and vice versa.

Input / Output in JAVA

Taking Input

We use input() method to read data in Python, similarly Java provides us various ways to take input from the user.

Simplest way to do this, would be to use the Console class in Java. There are two methods defined within System which help us read input using the console class. The methods are readLine() and readPassword().

Note: readPassword returns an array of characters for security reasons instead of a string

Here's an example to demonstrate the use of Console class, for the purpose of taking inputs

There are better ways to take inputs in Java, like the Scanner class which gives more control over which types of inputs do we want to take. For example taking a single integer or a complete line of text.

Printing Output

Similar to input, there are multiple ways by which we can output something in Java. Two of the most simple ways are: