Java Unit 1
Java Unit 1
● String literal
String s = “GeeksforGeeks”;
● Using new keyword
String s = new String (“GeeksforGeeks”);
Methods of String class
Big Numbers
⚫ If the precision of the basic integer and floating-
point types is not sufficient, we can use other classes
in the java.math package, called BigInteger and
BigDecimal.
⚫ These are classes for manipulating numbers with an
arbitrarily long sequence of digits.
⚫ BigInteger class is used for the mathematical
operation which involves very big integer
calculations that are outside the limit of all available
primitive data types.
⚫ The BigInteger class implements arbitrary precision
integer arithmetic, and BigDecimal does the same
for floating-point numbers.
Methods of java.math.BigInteger class
⚫ BigInteger add(BigInteger other)
⚫ BigInteger subtract(BigInteger other)
⚫ BigInteger multiply(BigInteger other)
⚫ BigInteger divide(BigInteger other)
⚫ BigInteger mod(BigInteger other)
Return the sum, difference, product, quotient, and
remainder of this big integer and other.
⚫ int compareTo(BigInteger other)
⚫ static BigInteger valueOf(long x)
⚫ Returns a big integer whose value equals x.
Reference:
https://www.informit.com/articles/article.aspx?p=10176
6&seqNum=9
Methods of java.math.BigDecimal
class
⚫ BigDecimal add(BigDecimal other)
⚫ BigDecimal subtract(BigDecimal other)
⚫ BigDecimal multiply(BigDecimal other)
⚫ int compareTo(BigDecimal other)
Returns 0 if this big Decimal equals other, a negative
result if this big decimal is less than other, and a
positive result otherwise.
⚫ static BigDecimal valueOf(long x)
Control Flow in Java
⚫ Java compiler executes the code from top to bottom.
The statements in the code are executed according
to the order in which they appear.
⚫ However, Java provides statements that can be used
to control the flow of Java code.
⚫ Such statements are called control flow statements.
Reference: https://www.javatpoint.com/control-flow-
in-java
Control Flow Statements
Array
An array is a collection of similar type of elements which has
contiguous memory location.
We can store only a fixed set of elements in a Java array.
Advantages
⚫ Code Optimization: It makes the code optimized, we can
position.
Disadvantages
⚫ Size Limit: We can store only the fixed size of elements
where
new - new keyword in Java
The new keyword is used to allocate memory at
runtime.
Example
Scanner sc=new Scanner(System.in);
Object Variable
A reference is nothing but a pointer pointing to the
object. In other words, it is holding the memory address
of the object created by new keyword.
Employee emp = new Employee();
In the above example, emp is a reference to the object
which is created using the new keyword.
Multiple object variables can exist which store the same
reference to an object's memory location.
Employee emp1=emp;
Both emp and emp1 are referring to the same memory
location
Accessor in Java
Accessor methods, also known as getter methods, are
methods that allow to retrieve the value of an object's
private instance variables.
These methods provide read-only access to the object's
state.
public class Person {
private String name;
public String getName() {
return name;
}
}
Mutator Methods in Java
Mutator methods, also known as setter methods, are
methods that allow you to modify the value of an
object's private instance variables.
These methods provide write-only access to the object's
state.
public class Person {
private String name;
public void setName(String name) {
this.name = name;
}
}
LocalDate class in Java
Java LocalDate class is an immutable class that represents
Date with a default format of yyyy-mm-dd.
This class belongs to java.time package
import java.time.LocalDate;
Methods of LocalDate Class
1. static LocalDate now() - It is used to obtain the current
date from the system clock in the default time-zone.
2. LocalDate minusDays(long daysToSubtract) It is used to
return a copy of this LocalDate with the specified
number of days subtracted.
3. boolean isLeapYear() It is used to check if the year is a
leap year, according to the ISO proleptic calendar system
rules.
Reference: https://www.javatpoint.com/java-localdate
Example Program
import java.time.LocalDate;
public class LocalDateExample1 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate yesterday = date.minusDays(1);
LocalDate tomorrow = yesterday.plusDays(2);
System.out.println("Today date: "+date);
System.out.println("Yesterday date: "+yesterday);
System.out.println("Tomorrow date: "+tomorrow);
}
}
Static Members
In Java, static members are those which belongs to the
class and we can access these members without creating
object of the class.
The static keyword in Java is mainly used for memory
management.
There is only one copy of static members in the memory
which can be shared among all the objects of the class.
The static can be:
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
Note: To create a static member(block, variable,
method, nested class), precede its declaration with the
keyword static.