This is the beginning of this Java course. Will focus on reviewing some object-oriented programming principles as well as introduce new content. While doing that, will learn Java syntax, and become more comfortable working with it. I will be quickly going through this course, however, as Iβm already fairly familiar with a lot of the concepts.
Jshell
This is sort of like vim, where is a terminal tool that allows us to run Java code.
Here, we can simply run some Java code, like such:
β Data Types
Has the usual βnumberβ data types:
- byte
- short
- int
- long
Of any of these data types, we can view the maximum, minimum, or size through some methods:
β Casting in Java
We can declare multiple variables of the dame data type in one line:
Consider the following code:
This will create an error, even though we know the value will fit in a byte. This is because at the time of compilation, the compiler does not know what the value of newMin is. So, since it does not know, it throws an error. On the contrary, if we did byte newValue = (Byte.MIN_VALUE / 2)
, we would no receive an error. If the calculation uses literal values, the compiler will be able to evaluate the result at run time, and so, no error is thrown. But, we know for sure that there value will fit into a Byte, so to override this error, we can use casting. Here is how we cast:
The byte in front of the division casts the result into a byte. This prevents Java from treating it as a default, which is an integer.
β Float & Double Primitives
- float
- double
These are the same as youβve used in C++. Javaβs default decimal data type is double. So, doing something like:
Will throw an error, with the compiler complaining that you cannot assign a double to a float. To make this a float, remember we need to add the suffix:
β Char and Bool
These two are the exact same as C++. Single quotes to define a character, and bool is just true or false.
β String
Unlike the things before which are primitive types, strings are Java defined classes.
The +
operator after a string always concatenates that following value onto the string.
β Operators, Operands, and Expressions
These are again, all the same as C++. Just note that something like:
β IntelliJ
Now, we are going to use this IDE moving forward.