
π΅Fundamental Java Concepts and Practices
Personalized Java learning notes encompass foundational concepts including types, loops, conditionals, input handling, arrays, strings, and essential operations
Java
Anatomy of JAVA:
Every code should have a function called
main.Function in a class is called METHODS.
Every code should have a class which is called
Main.fieldsare the variables in a class.private,public, andfinal.Access modifiers: means that a class or a function can use the class functions or their methods (Private & Public).
Naming Conventions:
PascalNamingConvention - Classes
camelNamingConvention - Methods
voidmeans this function is not going to return a value.PACKAGES > CLASSES > METHODS / FUNCTIONS.After running the compiler, the compiler turns the
main.javafile tomain.classfile which is BYTE CODE. That bytecode can run on every operating system that has JRE (Java Runtime Environment).JVM - Java Virtual Machine converts that ByteCode or the
.classfile into native operating system language, so that it can run that code.javac- for compiling the.javafile.java- to run the file. Write like this:com.package.Mainclass.sout
Java:
Java was developed by James Gosling in 1995.
The acquisition of Sun Microsystems by Oracle Corporation was completed on January 27, 2010.
JAVA EDITIONS:
Standard Edition (SE): This contains all libraries that a JAVA developer should know.
Enterprise Edition (EE): This is used to create industry-level applications. Also, it is based upon Standard Edition, but it contains more libraries that help to create high-level applications.
Micro Edition (ME): This is specifically designed for mobile devices and contains libraries that are necessary to build mobile devices.
Java Card (JC):
Types:
intis the type of the data, andageis called an Identifier (also called a Label) because that points to the data.=is the assignment operator.
Two Types:
Primitive Types: for storing simple values.
Reference Types: for storing complex values.
Primitive Types:
Byte
1
[-128, 127]
Short
2
[-32K, 32K]
Int
4
[-2.15B, 2.15B]
Long
8
[-9.22QT, 9.22QT]
Float
4
[-2.15B, 2.15B]
Double
8
[-9.22QT, 9.22QT]
Char
2
A, B, C, ...
Boolean
1
True, False
We can use _ to give space in numbers.
By default, JAVA sees every value as an integer. So, even if you give a long or double float, the Java compiler will think that this is an integer. Thus, you need to add a suffix, which goes like this: L for long.
Double takes 8 bytes and is too large. Prefer float for short values.
By default, a decimal number is taken as a double, so suffix it with F/f for float.
Default decimal values are also double, so add suffix F to represent float. This can be capital or small.
CHAR = 'A' & "Akash Kumar" β For single character, use single quotes; for words or big strings, use double quotes.
Reference Types:
In primitive Types, we store data like numbers, characters, and booleans. In Reference Type, we store complex data, like a mail message.
Declaring Reference Types is not like Primitive Types.
We have imported this Class, now we can use the members of this Object or Instance.
Now, Variable is an INSTANCE of Date Class.
They store the address of the date. Thatβs why itβs called Reference Types.
In primitive Types, if we do this, the value doesnβt change.
They store the address of the date. Thatβs why itβs called Reference Types.
In primitive Types if we do this, the value doesnβt change.
But in Reference Types, those x and y will point or address to the same address. That means if I change the values of one variable, the other will be changed and visible.
Because they are pointing to the same Address.
References Values are copied by their reference, the data and the changes are visible. In Primitive Types, the Values are independent.
Strings:
Even though we can declare the variable without the new operator thing, we can directly give out string like a primitive type, but at the end, this is a Reference Type.
We can use directly String message = "Hello world".
To Escape the Double Quotes we need to add \ as shown below:
For specifying the Backslash, we need to escape the same backslash with another .
IN Java, the strings are immutables, which means we canβt change them.
Any method that changes the values of the strings, gives us a new string or we can say a copy of that string.
Arrays:
Arrays are also Reference Types.
When we print the
arrayVariableName, it prints a string that is calculated based on the address of the object in memory.If we create another object or array and then print their value, we will get another value because they both are at different addresses in memory.
But if we want an output like in Python
[1, 2, 3, 4],We need to import
java.util.Arraysand then use a method calledArrays.toString()and pass the array name.This method gets the arguments from almost all primitive types and then prints the value like
[1, 2, 3, 4].
If we have initialized the value of the array, we will see that; if not, we will see 0 zeros in an integer array, empty strings
""in strings array, andfalsein a boolean array.We can assign the array with values at the time we are declaring that array, just like this, but only if we know the values:
int[] arr = {1, 2, 3, 4, 5, 5}
Arrays are of fixed length size; after initializing, we cannot change.
While Printing the Value of Multi-Dimensional arrays, use
Arrays.deepToString()then the name of the array.Use
finalfor constants, and for all constant variables, use uppercase naming convention. So instead of naming a variablepi, we will name the variablePI.
Dimensional Array
Example For TwoD Arrayss.
Arithmetic Operations:
The incrementor can be postfix
{x++}and prefix{++x}.This means if we do an assignment to a variable, like
y, and then use postfix, the value ofywill be the value ofxwithout being incremented, because the value that will be copied is not incremented. After the assignment, the value ofxwill be incremented.Now, as it defines prefix, it will first increment the value of
xand then assign it toy.We can use augmented operators like in Python in Java.
Casting:
Casting, Widening Casting, or Implicit Casting means casting a variable that is of lower values to higher variables' values so that we don't get any errors.
For example, if I have a
shortvariable, which means that I've allocated 2 bytes for that variable, then I try to assign the value of anintto it, which is a 4-byte memory, Java will see that the lower value variable can be added to the larger one.So, it will create an anonymous variable with the same value of that short number and then add it to the int.
byte>short>int>long>float>doubleWe can convert strings into other primitive types using the wrapper classes like
Integer.parseInt,Short.parseShort.When we pass the strings as arguments, it will give the value.
Math Class:
.floor.ceil.round.abs.minand.maxIt's straightforward, but we need to pay attention to casting.
Some methods are overloaded, meaning take care of the casting.
Math.random:
Store it to a
doublebecause it gives double values.It simply creates values from 0 to 1 in decimals.
If we want a number bigger than that, just multiply that with that number.
Sometimes, the casting will not work directly, but we know that. We will just cast them because the
Math.randomreturnsdouble, and we can't store it intointaccording to the compiler or IDE.But we will just cast that into
int.
NumberFormat
We cannot create a new instance of
NumberFormatbecause itβs an abstract class (just so you know).For now, we will use the methods of
NumberFormatto create an instance like this:NumberFormat.getCurrencyInstance();will return aNumberFormatobject, that's why the left side isNumberFormat.In the below code, we have used
currencymany times, but we don't exactly need that.We can use method chaining, so we will directly use the method like below:
Input Scanner
The
.nextwill store only the first.For a whole string, use
.nextLine.When we get a string from a value like
.next(), if we want to lowercase it, we can do that by usingstringVariable = scanner.next().toLowerCase().
Operators:
Comparison Operator:
==!=<>β€β₯
Logical Operators:
&&(AND)||(OR)
We can remove unnecessary curly braces when we have only one line command. We must use curly braces when we have a code block. Also, we canβt declare variables without them.
Simplifying If Statements:
Ternary Operator:
Switch Cases:
FizzBuzz:
While Loops
We cannot use comparison operators with reference types because these operators will compare the addresses of the left-hand side variables. Instead, we should use .equals("quit") or !anyStringVariable.equals("quit") for comparison.
We can use break to exit a loop and continue to skip the current iteration and move to the next one.
Using .equals() in the condition
Using 'break'
Simplifying the loop using 'while(true)' and 'break'
Example of 'break' and 'continue'
For Each Loop
One limitation of the for-each loop is that it does not support reversing the iteration.
The Main Things I've Waited For:
Use the following to verify or filter the input:
When a variable is declared in a code block, its scope is only within that block.
Unfinished Work:
Last updated
Was this helpful?