0% found this document useful (0 votes)
6 views33 pages

Java Programming: Operators & Objects Guide

Uploaded by

ouiam1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views33 pages

Java Programming: Operators & Objects Guide

Uploaded by

ouiam1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Oracle

Table

Contents
Chapter I : introduction to Java....................................................................3
Java doc....................................................................................................5
Ternary operator.......................................................................................7
Switch Construct.......................................................................................8
Switch expressions...................................................................................8
Yield value................................................................................................8
Jshell.......................................................................................................10
Chapiter III: Text, Date, Time and numeric objects....................................10
String......................................................................................................10
Texte blocks............................................................................................12
Method chaining.....................................................................................15
Local time and date................................................................................16
Localize resources...............................................................................19
Interning practice................................................................................20
How capacity is extended with string Builder......................................21
Use BigDecimal Class and Format Numeric Values.................................21
Round..................................................................................................21
Bigdecimal...........................................................................................21
Formatting...........................................................................................22
Time gap..............................................................................................24
Local time...............................................................................................25
Important Note:................................................................................25
Local date time.......................................................................................27
2
Chapter I : introduction to Java

3
4
Java doc

5
6
Ternary operator

7
Switch Construct

Switch expressions
Switch expressions were introduced in Java SE 14

Yield value

8
Scope of local variables switch statement vs switch exprression

9
Jshell

Chapiter III: Text, Date, Time and


numeric objects
String

10
every time we try to work with a String object, and try to change it, what's
going to
happen is that the new String has to be created, and the old one will be ga
rbage collected.
Well, if you are dealing with lots of String manipulation in this case, or mak
e changes to many, many, many Strings, that's going to basically create a
problem in terms of performance.
Why? Because every time you change a String, a new one has to be create
d by the
compiler, and the old one needs to be garbage collected. So if you're worki
ng with
thousands and thousands of Strings, that actually can affect the performan
ce of your system. So the question here, what would be the solution to act
ually avoid these type of headaches?
One answer is to use StringBuilder class
11
StringBuilder

Texte blocks

12
13
For example, here, I do have an SQL statement, where I wanted to enter th
e name of the city dynamically, so this represented by percentage s.

So I can apply, in this case, the formatted method and pass to with the city
, which is going to be assigned to this city here.

Wrapper classes

14
Method chaining

15
Local time and date

16
Represent languages and countries

17
H

18
Localize resources

19
Summary :
the complete table with Java Date, Time, Period, and Duration objects, including examples
and the results of the getters:

Object Example Code Common Getters / Getter Example &


Methods Result
LocalDate LocalDate x = [Link](2022, getYear(), [Link]() → 2022
3, 13);
getMonth(),
getDayOfMonth()
LocalTime LocalTime x = [Link](14, 30, getHour(), [Link]() → 14
15);
getMinute(),
getSecond()
LocalDateTime LocalDateTime x = getYear(), [Link]() → MARCH
[Link](2022, 3, 13, 14,
getMonth(),
30);
getDayOfMonth(),
getHour()
ZonedDateTime ZonedDateTime x = getZone(), [Link]() →
[Link]([Link]("Europ "Europe/Paris"
toLocalDate(),
e/Paris"));
toLocalTime()
OffsetDateTime OffsetDateTime x = getOffset(), [Link]() →
[Link](); toLocalDateTime() +01:00
OffsetTime OffsetTime x = [Link](); getHour(), [Link]() →
getMinute(), +01:00
getOffset()
Instant Instant x = getEpochSecond(), [Link]() →
[Link](1678901234); toEpochMilli() 1678901234
Period Period x = [Link](2, 3, 5); getYears(), [Link]() → 3
getMonths(),
getDays()
Duration Duration x = [Link](5); toHours(), [Link]() → 300
toMinutes(),
getSeconds()
ZoneId ZoneId x = getId(), [Link]() →
[Link]("Asia/Tokyo"); normalized() "Asia/Tokyo"
ZoneOffset ZoneOffset x = getTotalSeconds() [Link]()
[Link]("+02:00"); → 7200
Clock Clock x = [Link](); instant(), [Link]() → 2025-
millis() 02-19T13:30:15.123Z

20
Here’s the updated table with the change you requested (replacing [Link]()
with [Link]()):

Object Example Code Common Getters / Getter Example &


Methods Result
LocalDate LocalDate x = getYear(), [Link]() →
[Link](2022, 3, 13); 2022
getMonth(),
getDayOfMonth()
LocalTime LocalTime x = [Link](14, getHour(), [Link]() → 14
30, 15);
getMinute(),
getSecond()
LocalDateTim LocalDateTime x = getYear(), [Link]() →
[Link](2022, 3, 13, MARCH
e getMonth(),
14, 30);
getDayOfMonth(),
getHour()
ZonedDateTi ZonedDateTime x = getZone(), [Link]() →
[Link](2022, 3, 13, "Europe/Paris"
me toLocalDate(),
14, 30, 15, 0,
toLocalTime()
[Link]("Europe/Paris"));
OffsetDateTim OffsetDateTime x = getOffset(), [Link]() →
[Link](); toLocalDateTime +01:00
e
()
OffsetTime OffsetTime x = getHour(), [Link]() →
[Link](); +01:00
getMinute(),
getOffset()
Instant Instant x = getEpochSecond( [Link](
[Link](167890123 ), ) → 1678901234
4); toEpochMilli()
Period Period x = [Link](2, 3, 5); getYears(), [Link]() →3
getMonths(),
getDays()
Duration Duration x = toHours(), [Link]() →
[Link](5); 300
toMinutes(),
getSeconds()
ZoneId ZoneId x = getId(), [Link]() →
[Link]("Asia/Tokyo"); normalized() "Asia/Tokyo"
ZoneOffset ZoneOffset x = getTotalSeconds [Link]
[Link]("+02:00"); () () → 7200
Clock Clock x = [Link](); instant(), [Link]() →
millis() 2025-02-
19T13:30:15.123Z

Now, the ZonedDateTime is instantiated using [Link](). Let me know if you


need further adjustments!

21
Interning practice

22
How capacity is extended with string Builder

Use BigDecimal Class and Format Numeric Values


Round

This is how [Link]() works:

Bigdecimal

 [Link](rate) calculates the product of price and rate.

23
 The result from the multiplication is subtracted from the original
price.
 After performing the subtraction, the resulting BigDecimal value is rounded to 2
decimal places using the HALF_UP rounding mode.

 RoundingMode.HALF_UP means:
o If the digit after the rounding place is 5 or more, round up.
o Otherwise, round down.
 Example :
o 85.256 rounded to 2 decimal places becomes 85.26.
o 85.254 rounded to 2 decimal places becomes 85.25.

Formatting

This snippet sets up and customizes NumberFormat instances for different formatting
purposes in the French locale:

Currency formatting (currencyFormat) for values like 1 234,56 €.

Percentage formatting (percentFormat) with a precision of 2 fractional digits (e.g., 12,35 %).

Compact number formatting (compactFormat) for concise representations like 1,2 k.

24
Here is an example of 2 types local formatting FR vs GB

Use and Format Date and Time Values

This will display the day of 2022-12-08 (2021 + 1 year)

25
Time gap

Between function displays the time between 2 dates

You can get the gap time in minutes or hours using toMinutes() and toHours() functions

 toHoursPart(): Returns the hours part of the duration that does not roll into days.

 toMinutesPart(): Returns the minutes part of the duration that does not roll into hours.

 toSecondsPart(): Returns the seconds part of the duration that does not roll into minutes.

 toDaysPart(): Returns the days part of the duration (since this duration is less than 24
hours).

26
Local time
Current Time:

Specific Time:

Specific Time with seconds and nanoseconds:

Add or Subtract Time:

Adjust Specific Units:

This creates a new LocalTime instance with the specified hour and
minute, based on the current time.
For example, if the current time is 14:45:30.123, after the code runs,
adjustedTime will be 10:15:30.123

Important Note:

 The system time is not changed; the code only modifies the
representation of the time in your program.
 LocalTime is immutable, meaning each method ( withHour(),
withMinute()) returns a new LocalTime object, rather than modifying
the original one.

Extract Individual Components:

27
Check Before or After:

Compare Using compareTo:

Parsing from a String:

Custom Formatting:

Calculate Difference:

Catching Invalid Time Exceptions :

Special Cases

28
Method Description

Local date time


LocalDateTime is a class in the [Link] package, introduced in Java 8, and is part of the
Java Date-Time API. It represents a date-time without a time zone in the ISO-8601 calendar
system.

For example, 2024-12-12T15:30:45 represents a specific date and time but doesn't associate
it with any time zone (like UTC or PST).

Key Features

1. Immutability: LocalDateTime is immutable, meaning once it's


created, it cannot be changed. Methods like plusDays() or
minusHours() return a new LocalDateTime instance instead of modifying
the existing one.
2. Time Zone Neutral: It doesn't hold time zone information. If you
need time zones, you can use ZonedDateTime or OffsetDateTime.
3. Precision: Includes date (year, month, day) and time (hour, minute,
second, nanosecond).

29
Current Date-Time:

Specific Date-Time:

Parse from String:

Adding or Subtracting Time:

Adjusting Specific Fields:

Get Date or Time Components:

Check if One Date-Time is Before or After Another:

30
Summary

ZonedDateTime

ZonedDateTime is a class in the [Link] package, introduced in Java 8, and it represents a


date-time with a time zone. This is helpful when you need to handle date and time values in
different time zones.

For example, 2024-12-12T15:30+05:30[Asia/Kolkata] represents a date and time in the


Indian time zone (UTC+5:30)

Current Zoned Date-Time:

Specific Zoned Date-Time:

Converting from LocalDateTime:

Adding or Subtracting Time:

31
Changing Time Zone:

Time Zone and Offset:

Date and Time Components :

32
Chapter III: classes and objects
UML

Modeling classes

33

You might also like