Intention behind creating java was to create a language that can run anywhere and everywhere. That's why you see the oracle's message that java runs on 1B Devices So they were quite successful in doing that.
Java is actually a language specification, and there are multiple implementations of it
Java(1995) is a much younger language than python(1991) then the natural question is why is python more feature rich and has more libraries than java. Now that's a quiz question for tomorrow
Java is
.java files to .class file
and .class file is executable prettymuch anywhere a JVM can run it's portable .class
files will be executable anywhere.java files and external libraries seperate
adding an additional layer of security, Bytecode verifier checks if there's any invalid code or
malicious code even before executing the code and secuirty manager monitors each class for things
like disk read or write..class file which is executed by JVM
making the language interpretedHow Java works
[.java file] -- Compiler (javac) -> [.class file] -- JVM -> Output
How JVM works
[.class files] -- [Class Loader] -> [Byte code verifier] -> [Intrepreter] -> [Runtime] -> Hardware -> Output
JVM Architecture
$JAVA_HOME/jre/lib/ext-cp or -classpath argument to java command
when executing a classnew
keyword it is stored in heap. Note that this heap has nothing to do with datastructure min/max heap
even though it has the same name.undefined in case of native methodWhat's a Path Path is a variable that helps OS find where an executable is. This helps us run the executable in terminal, without having to remember the whole file path for that executable.
Type of variables
Data types
boolean -- 1 bytebyte -- 1 byteschar -- 2 bytes -- java uses 2 bytes in char because it uses
unicode system not ASCII. short -- 2 bytesint -- 4 bytes - integer literals are int by defaultlong -- 8 bytesfloat -- 4 bytesdouble -- 8 bytes -- decimal literals are double by defaultOperators
| Operator Type | Category | Precedence |
|---|---|---|
| Unary | postfix | expr++ expr-- |
| prefix | ++expr --expr +expr -expr ~ ! | |
| Arithmetic | multiplicative | * / % |
| additive | + - | |
| Shift | shift | << >> >>> |
| Relational | comparison | < > <= >= instanceof |
| equality | == != | |
| Bitwise | bitwise AND | & |
| bitwise exclusive OR | ^ | |
| bitwise inclusive OR | | | |
| Logical | logical AND | && |
| logical OR | || | |
| Ternary | ternary | ? : |
| Assignment | assignment | = += -= *= /= %= &= ^= |
Important Keywords when coming from C
class -- Java class keyword is used to declare a class.implements -- Java implements keyword is used to implement an interface.
instanceof -- Java instanceof keyword is used to test whether the object
is an instance of the specified class or implements an interface.interface -- Java interface keyword is used to declare an interface. It
can have only abstract methods.native -- Java native keyword is used to specify that a method is
implemented in native code using JNI (Java Native Interface).new -- Java new keyword is used to create new objects.null -- Java null keyword is used to indicate that a reference does not
refer to anything. It removes the garbage value.package -- Java package keyword is used to declare a Java package that
includes the classes.strictfp -- Java strictfp is used to restrict the floating-point
calculations to ensure portability.synchronized -- Java synchronized keyword is used to specify the critical
sections or methods in multithreaded code. this -- Java this keyword can be used to refer the current object in a
method or constructor.throw -- The Java throw keyword is used to explicitly throw an exception.
The throw keyword is mainly used to throw custom exceptions. It is followed by an
instance.throws -- The Java throws keyword is used to declare an exception. Checked
exceptions can be propagated with throws.try -- Java try keyword is used to start a block of code that will be
tested for exceptions. The try block must be followed by either catch or
finally block.catch -- Java catch keyword is used to catch the exceptions generated by
try statements. It must be used after the try block only.finally -- Java finally keyword indicates a block of code in a
try-catch structure. This block is always executed whether an exception is handled or
not.super -- Java super keyword is a reference variable that is used to refer
to parent class objects. It can be used to invoke the immediate parent class method.4.11 Distributed -- Java allow to create distributed applications which can connect with eachother over a network to execute a task
9.1.3 char -- 2 bytes -- java uses 2 bytes in char because it uses unicode system not
ASCII. | If you need a primer on ascii vs unicode ask.
7.1 ClassLoader
$JAVA_HOME/jre/lib/ext-cp or -classpath argument to java command
when executing a classCat and Dog class inherit Animal class. Each of them can
override Animal Class's String talk() method to return meow or
Woof as long as they take no input and return string.| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| no modifier | Y | Y | N | N |
| private | Y | N | N | N |