Section 01 Java Fundamentals
Section 01 Java Fundamentals
© luv2code LLC
Course Development Team
Eric Roby
© luv2code LLC
What is Java?
Embedded Systems
Automotive, network routers, Games
medical devices, … Minecraft, Runescape, …
• Documentatio
n
• News
https://dev.java
© luv2code LLC
Java Development
• Java Integrated Development Environment (IDE
• You can also use the Ultimate Edition ($) ... free trial version is available
• IntelliJ is available fo
• Microsoft Window
• Ma
c
• Linux
• We’ll show how to con gure the JDK for IntelliJ in upcoming videos
fi
www.luv2code.com © luv2code LLC
Development Steps
1. Install Intelli
© luv2code LLC
Basic Java Program
• Program Structur
• main metho d
• Displaying Tex t
• Java Keywords
}
Even on operating systems that are
not case-sensitive
System.out.println("Hello World!")
}
Hello World!
fi
www.luv2code.com © luv2code LLC
Java Keywords
Category Keywords
Access Modifiers public, protected, private
final, static, abstract, synchronized, volatile,
Class, Method and Variable Modifiers transient
int, char, byte, short, long, float, double,
Primitive Types boolean
if, else, while, do, for, break, continue,
Control Flow Statements return, switch, case, default
Exception Handling try, catch, finally, throw, throws
class, interface, extends, implement, package,
Class Definition import, record
Objects new, this, super, instanceof
void, enum, assert, native, others …
Others goto, const
© luv2code LLC
Displaying Text
System.out.println(" … ");
File: Demo.java
System.out.println("Hello World!")
}
Hello World!
fi
Assign a String to a variable
System.out.println(message);
• The Java documentation lists the methods / functions that are available for the String class
Method Description
length() returns the length of the String
toUpperCase() returns an upper case version of the String
toLowerCase() returns a lower case version of the String
… …
https://docs.oracle.com/en/java/javase
© luv2code LLC
Earlier Example
File: Demo.java
is hard coded
System.out.println("My favorite color is blue")
}
My favorite color is blue
My hobby is coding
is hard coded
String theColor = "blue"
… but a little bit better
My hobby is coding
Create a new instance that Configure the scanner to read user input
we can use in our app typically from the keyboard
scanner.close();
Best practice
© luv2code LLC
Primitive Data Types
• A primitive data type is a basic type provided by Jav
• Data types are de ned with a xed size and data range
fi
fi
www.luv2code.com © luv2code LLC
Primitive Data Types
Name Description
data = 10
2
info = 1300
0
stockCount = 214748364
6
cosmicDistance = 9223372036854775807
• Utility methods for working with primitive data types: for example, convert String to in
System.out.println("interestRate=" + interestRate)
System.out.println("prizeWinning=" + prizeWinning);
interestRate=7.9
prizeWinning=9.98234245245E9
Scientific e-notation
System.out.println("stage=" + stage)
System.out.println("alive=" + alive)
System.out.println("found=" + found );
stage=
alive=tru
found=false
System.out.println("Formatted average: " + formattedAvg); with (2) digits to right of decimal point
© luv2code LLC
Earlier Example
grades are
double gradeExam1 = 87.5
hard coded
Output is no longer
Grade exam 2: 54. hard coded
0
© luv2code LLC
Casting and Conversion
• You may need to convert a oating point number to an intege
fl
r
• or vice-versa
• Java support
s
byte: 8-bit
• Floating point numbers are more precise than integer numbers short: 16-bit
int: 32-bit
long: 64-bit
• For example
:
short: 16-bit
int: 32-bit
• Analogy: smaller items can ALWAYS t inside larger box long: 64-bit
fi
byte data = 102
;
fl
n
// convert double to in
System.out.println("theDoubleGradeAvg=" + theDoubleGradeAvg)
System.out.println("theIntGradeAvg=" + theIntGradeAvg);
theDoubleGradeAvg=89.
theByteDistance=123
System.out.println("theFloatDistance=" + theFloatDistance)
System.out.println("theByteDistance=" + theByteDistance)
int theCharacterCode = 65
;
System.out.println("theCharacterCode=" + theCharacterCode)
System.out.println("theChar=" + theChar);
theCharacterCode=6