Java Module (Minggu 4)
Java Module (Minggu 4)
Java Module (Minggu 4)
best suited for internet programming. Later, Java technology was incorporated by
Netscape.
The principles for creating Java programming were "Simple, Robust, Portable,
Platform-independent, Secured, High Performance, Multithreaded, Architecture
Neutral, Object-Oriented, Interpreted, and Dynamic". Java was developed by James
Gosling, who is known as the father of Java, in 1995. James Gosling and his team
members started the project in the early '90s.
Prerequisite
To learn Java, you must have the basic knowledge of C/C++ programming language.
Audience
Our Java programming tutorial is designed to help beginners and professionals.
Problem
We assure that you will not find any problem in this Java tutorial. However, if there
is any mistake, please post the problem in the contact form.
If Java is installed, you will see something like this (depending on version):
If you do not have Java installed on your computer, you can download it for free
at oracle.com.
Note: In this tutorial, we will write Java code in a text editor. However, it is possible
to write Java in an Integrated Development Environment, such as IntelliJ IDEA,
Netbeans or Eclipse, which are particularly useful when managing larger collections
of Java files.
Don't worry if you don't understand the code above - we will discuss it in detail in
later chapters. For now, focus on how to run the code above.
Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe),
navigate to the directory where you saved your file, and type "javac Main.java":
This will compile your code. If there are no errors in the code, the command prompt
will take you to the next line. Now, type "java Main" to run the file:
ch1 contains X
ch1 is now Y
3.1.4 Boolean
The boolean type can have one of two values, true or false.
class BoolTest
{
public static void main(String args[])
{
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
if(b)
System.out.println("This is executed.");
b = false;
if(b)
System.out.println("This is not executed.");
System.out.println("10 > 9 is " + (10 > 9));
}
}
output:
b is false
b is true
This is executed.
10 > 9 is true
3.2 Variable
A variable provides us with named storage that our programs can manipulate.
Each variable in Java has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and
the set of operations that can be applied to the variable.
You must declare all variables before they can be used. Following is the basic form
of a variable declaration −
Here data type is one of Java's datatypes and variable is the name of the variable. To
declare more than one variable of the specified type, you can use a comma-separated
list.
This chapter will explain various variable types available in Java Language. There are
three kinds of variables in Java −
• Local variables
• Instance variables
• Class/Static variables
}
This will produce the following result −
Output
Puppy age is: 7
Example
Following example uses age without initializing it, so it would give an error at the
time of compilation.
Output
Test.java:4:variable number might not have been initialized
age = age + 7;
^
1 error
3.2.2 Instance Variables
• Instance variables are declared in a class, but outside a method, constructor or
any block.
• When a space is allocated for an object in the heap, a slot for each instance
variable value is created.
• Instance variables are created when an object is created with the use of the
keyword 'new' and destroyed when the object is destroyed.
• Instance variables hold values that must be referenced by more than one
method, constructor or block, or essential parts of an object's state that must
be present throughout the class.
• Instance variables can be declared in class level before or after use.
Example
Live Demo
import java.io.*;
public class Employee {
Output
name : Ransika
salary :1000.0
Example
import java.io.*;
public class Employee {
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
Output
Development average salary:1000
Note − If the variables are accessed from an outside class, the constant should be
accessed as Employee.DEPARTMENT
3.3 Conversion
Type conversion is simply the process of converting data of one data type into
another. This process is known as type conversion, typecasting, or even type coercion.
The Java programming language allows programmers to convert both primitive as
well as reference data types
3.3.1 Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically
converted. This happens when:
• The two data types are compatible.
• When we assign a value of a smaller data type to a bigger data type.
For Example, in java, the numeric data types are compatible with each other but no
automatic conversion is supported from numeric type to char or boolean. Also, char
and boolean are not compatible with each other.
Example:
class GFG {
}
}
Output:
Int value 100
Long value 100
Float value 100.0
3.3.2 Narrowing or Explicit Conversion
If we want to assign a value of a larger data type to a smaller data type we
perform explicit type casting or narrowing. This is useful for incompatible data types
where automatic conversion cannot be done. Here, the target type specifies the
desired type to convert the specified value to.
char and number are not compatible with each other. Let’s see when we try to
convert one into another.
Example:
// Java program to illustrate Incompatible data Type
// for Explicit Type Conversion
// Main class
public class GFG {
This error is generated as an integer variable takes 4 bytes while character datatype
requires 2 bytes. We are trying to plot data from 4 bytes into 2 bytes which is not
possible.
How to do Explicit Conversion?
Example:
// Java program to Illustrate Explicit Type Conversion
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Double datatype
double d = 100.04;
// Explicit type casting by forcefully getting
// data from long datatype to integer type
long l = (long)d;
// Explicit type casting
int i = (int)l;
// Print statements
System.out.println("Double value " + d);
// While printing we will see that
// fractional part lost
System.out.println("Long value " + l);
// While printing we will see that
// fractional part lost
System.out.println("Int value " + i);
}
}
Output
Double value 100.04
Long value 100
Int value 100
Note: While assigning value to byte type the fractional part is lost and is reduced to
modulo 256(range of byte).
// Print commands
System.out.println("d = " + d + " b= " + b);
}
}
Output
Conversion of int to byte.
i = 257 b = 1
d = 323.142 b= 67
Output
Example
In the last example, the Unicode code sequence for \u2122 produces a trademark
symbol (™)
When you use a string literal in your Java program, Java automatically creates an
instance of the class String for you with the value you give it. Strings are unusual in
this respect; the other literals do not behave in this way (none of the primitive base
types are actual objects), and usually creating a new object involves explicitly
creating a new instance of a class.
\t – tab
\b – backspace (a step backward in the text or deletion of a single character)
\n – new line
\r – carriage return
\f – form feed
\’ – single quote
\” – double quote
\\ – backslash
// create a string
String greet = "Hello! World";
System.out.println("String: " + greet);
Output:
String: Hello! World
Length: 12
In the above example, the length() method calculates the total number of characters
in the string and returns it.
Output:
First String: Java
Second String: Programming
Joined String: Java Programming
In the above example, we have created two strings named first and second. Notice
the statement,
We can also join two strings using the + operator in Java. To learn more, visit Java
String concat().
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
Output
In the above example, we have created 3 strings named first, second, and third. Here,
we are using the equal() method to check if one string is equal to another.
The equals() method checks the content of strings while comparing them. To learn
more, visit Java String equals().
Methods Description
replace() replaces the specified old character with the specified new chara
References:
1. https://www.javatpoint.com/history-of-java
2. Java Getting Started (w3schools.com)
3. Source : https://www.glassdoor.com/Salaries/java-programmer-salary-
SRCH_KO0,15.htm
4. https://www.tutorialspoint.com/java/java_variable_types.htm
5. Java String (With Examples) (programiz.com)