Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ly/
java IDE
is a tools we need to build java application/programs
jre and jdk are two parts used to create and run java application
wizard --a help feature of a software package that automates complex tasks by
asking the user a series of easy-to-answer questions.
there is a utility called java doc that allow us to generate documentation using
special comment format
allow basically to write your code documentation right inside sc and when you run
java doc utility n if you follow appropriate structure in comment that
uility will generate documentation right from sc
cast---pure, cut
implicit conversion
short,long---long
float,double---double
long ,double--double
explicit
int---float
loose significant digit bcz of how
it is stored
conditional logic
our program will behave differently based on differences in data values.
if statement,
which allows us to change our code path in our application
if else statement allows us to conditionally execute another statement
logical operators
that allow us to combine different conditions together.
A conditional assignment
allows us to assign a value to a variable based on the result of some condition
condi- season
claus- shut,close----closing condition/statement
demo if else
logical operators are used to work on the results of other relational operations.
A while loop
allows you just to repeatedly execute some statement as long as the condition is
true.
factorial--fact--do, make
for each--executing that loop or the statement within the loop once for each member
in the array
Switch
transfers control to a particular statement based on a value, and its purpose
is to simplify the idea of testing against multiple matches.
it support char ,integer and string
objects are these things that encapsulate the data, operations, and usage
semantics.
what you want to work on and how you work with it
initialization blocks.
used to share code across constructors
or
Initialization blocks allows us to run code that can be shared potentially across
constructors.
signature(contructor):The key thing is that just the number and types of the
parameters have to
differ across various constructors.
constructor visibility
"Initialization Blocks". they allow us to create code that is shared across all our
constructors
What they do is they run the code as if that code were placed at the very
beginning of each constructor.
overloading, the ability to use different parameter lists with a given method name
or the constructor.
eg juice with apples ,oranges etc
super keyword, a useful tool for accessing base class behaviors in a derived
class.
Java allows one class to inherit from another class, also known as having a class
derive from another class.
What happens is that that derived class has the characteristics of the base class,
but then can add its own specializations to create more capabilities
In Java, all
methods are automatically overridable unless you do something special to say that
you don't want them overridden.
override annotation.
And what this tells the compiler is that it is your intention to override a method
from the base class.
So the compiler will actually check and make sure that the base class has a method
with that signature.
So it's kind of just a sanity check to make sure that the method you're declaring
really does replace a
method in the base class. It has no impact on runtime. It's only used at compile
time.
every class in Java, whether it's a built-in class or one you create has the
characteristics of the Object class.
The Object class actually has methods that are defined on it, so that means that
there are
specific characteristics and capabilities that are common to all classes.
when we have a reference of a given type, we can only access the capabilities that
are visible to that reference.
hashCode()
It returns back a hash code(may be object no) that's useful for working with things
like hash tables(object bucket) and other types that rely on hash tables.
scratch card
object class
6 methos imp
super
Like this, super provides access to the current object.
But where super is different is that super treats that object
as if it's an instance of its base class,
and it allows us to
access the features of the object as if our current class has
not overridden any of those features. And this comes in really
handy when we're implementing something in our current class and
we want to get to the features or a method exactly as they behaved in the base
class.
default, all classes can be extended, and when you inherit from a class,
you have the option of using the methods as they were written in the base class or
overriding them.
In the case of final, you'll generally either mark the class as final or an
individual method as final. Because once a class is final, there's no reason to
mark individual methods as final because you can't override anything if you can't
inherit from it.
Abstract allows you to require that a class be inherited and require that a
particular method be overridden
So it allowed us to define basic characteristics and behaviors in the class,
but then require specific specializations in any derived class.
when you see code like this where you've got a big condition like this and there's
individual tasks inside of there,
as a program gets more complex and you start to work inside this method, this is a
place where, very often, enhancing
or modifying the program will introduce errors because we've got a bunch of kind
of loosely related code kind of
clumped together. This is a great opportunity for us to use the class inheritance
features of Java to have a better
division of responsibilities
switch --->subclass
inheritance--
herit--choose, take
in--in
take in
we can bring the capability of classes to primitive values using the primitive
wrapper classes.
enumeration types, which allow us to define types that can only hold a finite list
of values.
en----in
numer- number
enumerate
mention (a number of things) one by one.
String class
It stores a sequence of Unicode characters.
It stores them using the UTF-16 encoding,
which basically means that the Java String class can store pretty much any
character
you're likely to ever want to work with.
Example :
Int A;
A=20;
concatenation
con--with, together
caten-- chain
chain together
string
format
form--shape
at--addition
make some changes in shape
https://app.pluralsight.com/player?course=java-fundamentals-language&author=jim-
wilson&name=java-fundamentals-language-m9&clip=1&mode=live
length-- https://www.javatpoint.com/java-string-length
valueOf- https://www.javatpoint.com/java-string-valueof
concat-- https://www.javatpoint.com/string-concatenation-in-java
replace- https://www.javatpoint.com/java-string-replace
toUpperCase https://www.javatpoint.com/java-string-touppercase
toLowerCase https://www.javatpoint.com/java-string-tolowercase
trim https://www.javatpoint.com/java-string-trim
split https://www.javatpoint.com/java-string-split
charAt https://www.javatpoint.com/java-string-charat
substring https://www.javatpoint.com/substring
contains https://www.javatpoint.com/java-string-contains
endsWith https://www.javatpoint.com/java-string-endswith
startsWith https://www.javatpoint.com/java-string-startswith
indexOf https://www.javatpoint.com/java-string-indexof
lastIndexOf https://www.javatpoint.com/java-string-lastindexof
CompareTo and-------https://www.javatpoint.com/java-string-compareto
compareToIgnoreCase---
https://www.tutorialspoint.com/java/java_string_comparetoignorecase.htm
check to see if one string is greater than, equal to, or less than another string
based on the Unicode character sequences.
IsEmpty -------------https://www.javatpoint.com/java-string-isempty
tells us whether we have a zero-length string
intern
The intern method gives us back a canonicalized reference of a string value.
when you call the intern method on a string value, it will always return back the
same exact string object for a given string value
the intern assures that two strings with the same value will reference the exact
same object.
capacity()
append()
insert()
append, which lets us add new content to the end of the StringBuilder,
insert, which gives us the ability to add new content within the body of the
StringBuilder.
capacity says how much space is allocated, length says how much is in use.
classes also have overhead. Every class instance has a certain amount of memory
that's always taken up just by the fact that it's a class, and that's before
it even has its own specific values inside of there
isInfinite https://www.javatpoint.com/java-double-isinfinite-method
isNAN not a number https://www.javatpoint.com/java-double-isnan-method
isDigit https://www.javatpoint.com/post/java-character-isdigit-method
Check whether a particular codePoint is a digit or not.
isletter https://www.javatpoint.com/post/java-character-isletter-method
Enumeration types
allow us to define a type that has a list of valid values.
string-->primitive
use parseXX method
like Double.parseDouble("")
https://www.quora.com/Can-you-explain-Javas-error-and-exception-hierarchy
e.getMessage
The getMessage method gives us back the appropriate message for that exception.
e.printStackTrace()
we can even print out a stack trace showing exactly what went wrong.
FileReader is a built-in class that takes care of the details of reading content
from a file.
BufferedReader is something that adds some buffering on top of that to make reading
from a file more efficient.
It also provides some helper methods.
https://stackoverflow.com/questions/7376647/what-is-the-difference-between-javas-
bufferedreader-and-inputstreamreader-class
any error that occurs, that inherits from this class Error,
is generally one we don't have to think about too much in our code
because it generally represents some kind of problem directly inside
the Java virtual machine. So because of that, we don't really handle
those very often. An example of one of these might be like a linkage error.
A linkage error occurs when you try to load a class, and it's not able
to load that class up for some reason
Now the errors that we tend to interact with much more inherit from a class
Exception
area where we work most commonly are the classes that inherit more directly from
the Exception class,
and these are the things we want to actually watch out for in our programs.
eg
IOException where you're trying to interact with a file and you're not able to,
Now these exceptions on this side here are so important that we actually handle
them correctly.
They're considered checked exceptions. A checked exception means that the compiler
actually looks,
and anytime one of these exceptions is thrown, the compiler looks to see if you
actually handled it.
If you do not handle a checked exception, the compiler will raise an error.
In other words, it won't let you build your program. So you have to handle all
checked exceptions.
Now the exceptions here under RuntimeException are considered unchecked exceptions
meaning that you
can handle them, but the compiler doesn't require that you do. So even if the
compiler knows that it's
possible for that exception to be thrown, if you don't handle it, it's not going
to raise an error.
The way you can tell an unchecked exception from a checked exception, any
exception class that
inherits from RuntimeException is considered an unchecked exception.
Any class that inherits from Exception but not RuntimeException is considered a
checked exception. Now the virtual machine-related errors,
those are treated as unchecked exceptions. In other words, the compiler does not
require that you handle those.
When an exception occurs, the Java environment will start testing your catch blocks
starting at the top and walking down each one.
The first catch block that it encounters that is assignable for this exception is
used. And what we mean by assignable is that
the catch block handles an exception that is either the type of the exception
thrown or the base class of the type of exception thrown.
Now because a base class can satisfy the catch statement, we want to make sure our
catch blocks start with the most specific
exception types first and get more general as we go down
IOException, a problem interacting with a file, and there might even be a
FileNotFoundException if I try to open a file that doesn't exist.
So far, we've been looking at how exceptions work within a specific block of code.
Let's look now how they work when we call down from multiple methods
https://www.quora.com/In-laymans-terms-what-is-the-difference-between-the-heap-and-
the-stack-in-JAVA
https://www.quora.com/Why-method-area-and-heap-area-both-are-sharable-in-java
https://www.quora.com/How-and-where-are-methods-stored-in-Java
https://www.quora.com/Why-primitive-instance-variables-are-stored-in-stack-and-
reference-variables-objects-stored-in-heap-in-computer-memory-Like-how-was-this-
logic-thought-to-store-one-in-stack-and-other-in-heap-in-languages-like-net-c++-
java
https://www.quora.com/How-and-where-are-methods-stored-in-Java
https://www.quora.com/Java-If-we-create-an-object-in-a-method-will-the-object-be-
stored-in-the-stack-or-in-the-heap
https://www.quora.com/What-is-the-reason-local-variables-are-stored-in-stack
https://www.quora.com/If-Java-objects-are-stored-on-the-heap-memory-do-the-methods-
of-a-particular-object-also-reside-on-the-heap-memory-and-not-the-stack-memory
D does something that throws an exception. So now that exception occurs, and now
something has
to try to handle that exception. So what happens is the system starts trying to
see if it can
be handled. Since methodD doesn't handle it, it went to start looking up the call
stack to find
some code that can actually handle this exception.
D does something that throws an exception. So now that exception occurs, and now
something
has to try to handle that exception. So what happens is the system starts trying to
see if
it can be handled. Since methodD doesn't handle it, it went to start looking up the
call stack
to find some code that can actually handle this exception.
Well, remember here in methodA we put that try/catch in place. Well that catch
puts a marker
on the stack. So what happens now is that Java looks up the call stack, and when it
finds that
marker, it says oh, you can handle this exception. The call stack is cleaned up,
and then control
is passed to that catch block. So that means then, of course, is that exceptions
propagate up the
call stack, meaning that they can actually cross method boundaries.
One is it can catch the exception, but it doesn't have to catch the exception.
But if it doesn't catch the exception, then it must document that that exception
might occur.
The way that's documented is that a method can have a throws clause on it, and that
throws clause
needs to list any checked exceptions that might occur within that method that it
doesn't handle
So any try/catch they put in place there, they're going to catch the exceptions
that are defined on
Flight's implementation of addPassengers. We want to make sure that our derived
class doesn't start
throwing exceptions that the user of our class didn't know they were responsible
to handle.
In addition to being able to handle exceptions as we've done up until now, our code
can actually
throw exceptions as well, and that would just be in any case where we want to
signal that an error
had occurred. So we might throw an exception because our code has identified
something wrong with
the data we're working on, or it might even be that our code has caught an
exception and wants to
throw a different exception to provide more information.
idea of tying one exception to another is very fundamental to the exception classes
All the exception classes have a method called initCause that will allow you to
associate an exception with an instance of another exception
in most cases, you're going to want to use one of the existing exception types.
But if you really have a situation where you've got something you need to provide
information
on that doesn't really fit one of the existing exceptions, you can define your own.
ou'll generally want to have at least two constructors. You'll have one constructor
that
accepts whatever the required detail is. That may be a simple string message. It
may be
some meaningful fields. You'll generally also want to have another constructor
that accepts
that same detail along with an originating exception, again, so that if this
exception's thrown
in response to a different exception, they can be tied together.
And you can, of course, have more constructors than that if you want to. You can
have a no-argument constructor,
you can have whatever you need, but you'll generally have at least these two.
Type imports provide a way for us to tell the compiler how to map simple type names
to qualified names. We do that using the import keyword. Now one thing I want to
note
is that the keyword import may kind of give you the sense of direction of kind of
loading
something in, and that's not what import does. Import is just a mapping for the
compiler.
import on demand
it allows the compiler to map in all the types from within a package.
a package is generally a group of related types, and those types will provide some
kind of related functionality.
By working with packages, our software gets built into a predictable structure, and
that predictable structure simplifies this idea of distribution
Archive files allow us to put that folder structure into a file, and these are
commonly known as JAR files.
a manifest,
and that manifest provides information about what's going on inside that archive.
And basically, it's just a bunch of name value pairs,
artifacts are other things you can produce from the project
A JAR file is considered an artifact in IntelliJ.
JAR file really is just a ZIP file with the addition of that manifest.
So we can have types and members that are visible inside of the package, but not
outside of the package so they can provide support for the capabilities of that
package without being directly usable on their own.
when you implement the interface, the type that you pass in doesn't have to be the
same type as the class that's implementing it.
Now the constants we put on an interface are always considered public, final, and
static.
We don't have to use those keywords, but they're treated that way. What that means
is that
any constant you associate with an interface has the same value for all
implementers of the
interface. There's no concept of a per-instance constant in interfaces. It's
always interface-wide,
or true for all implementers of that interface
inner classes
where we actually can have the nested class's instance automatically associated
with an instance of its enclosing class.
anonymous classes,
the ability to declare and create a class all at one time.
There are times in our applications where we need to do some kind of work to
prepare a class before we use it,
and that's where static initialization blocks come in.
Well, static initialization blocks execute before you actually even start using
the type.
You can kind of think of those as type initializers.
So these static initialization blocks are this really powerful capability that
takes care of initializing our classes prior to their first use with no special
calls on the part of the user.
nested type actually has visibility of private members of its enclosing type
you can also have a nested class that is protected, being that it would only be
accessible by classes
that inherit from its enclosing class.
nested type
2 parts
1part
for structure and (want to structure its naming relative to another class).
We could even kind of create the name of one class to be structured inside of
another.
scoping(make the type only usable in certain scenarios). scoping where we can limit
who can access the type
In this case, there's no special relationship between instances of the nested class
and instances of the enclosing class.
It's really because you want to make the type only usable in certain scenarios or
you want to structure its naming relative to another class
inner classes.
We can get a much closer relationship when we nest one class inside of another.
There's a concept called inner classes.
you actually have a class who each instance of which is associated with an
instance of its enclosing class,
so there's just kind of an implicit relationship between instances.
the FlightIterator actually has two this references. It has the traditional this
reference, the ability to access its own members,
but it also has a Flight.this reference, which allows it to access the members of
the instance of Flight in which it was created.
java provides the ability to create anonymous classes. With anonymous classes,
rather than use the class keyword
to go ahead and give a class a name, to give its declaration, then later go off
and create it, with anonymous classes,
we're able to declare our class as part of the class's creation.
Well that FlightIterator class is not used anywhere else in our program except
within this one method, Iterator.
So this is actually a great chance for us to use an anonymous class to kind of
further make our code a little bit more concise
So anonymous classes allow us to avoid the need to go off and declare these bunch
of classes and give them names
that we're only using in one place. Instead, right here where we need it, we take
care of the entire job of declaring
the class and creating it.
links
1.https://www.oracle.com/technetwork/articles/java/index-jsp-135444.html
2.https://stackoverflow.com/questions/415687/why-are-local-variables-not-
initialized-in-java good
3.https://stackoverflow.com/questions/19443524/why-java-is-asking-to-initialize-
the-variables-when-it-is-local pure answer must read can skip others
This is a problem with what the compiler can know about.
In the case of the Main Method, the compiler knows for sure that the variables have
not be initialized.
But in the case of the printTest methodwhich is a method of a class , the compiler
does know that
there could be some other method (or same package class) which initialized the
class variables before using it
5.https://stackoverflow.com/questions/1560685/why-must-local-variables-including-
primitives-always-be-initialized-in-java pure gold i like it
6.https://stackoverflow.com/questions/42269305/why-the-local-variables-can-not-be-
initialized-with-default-values
7.https://blogs.msdn.microsoft.com/ericlippert/2009/10/12/absence-of-evidence-is-
not-evidence-of-absence/ you cant skip this plz plz
compiler is not that sophisticated to know whether a variable is definitely
initialize so local variable must be initilize to avoid error/bug
so how compiler knows whether we actually asssign value before use ---compiler
assign memory area equal to data type so while using that variable it
enforces rule value must be put before use.
but in case of class variable same case but one caveat is if we let user to
initialize it can be overridden by constructor so its better to
use default zero value to avoid error and overriddden value
8.https://softwareengineering.stackexchange.com/questions/355059/how-are-variables-
stored-in-a-language-compiler-or-interpreter
9.https://stackoverflow.com/questions/1560685/why-must-local-variables-including-
primitives-always-be-initialized-in-java imp
10.https://stackoverflow.com/questions/1542824/initialization-of-instance-fields-
vs-local-variables/1542851#1542851 imp
11.https://stackoverflow.com/questions/29967741/what-happens-in-memory-if-we-just-
declare-a-variable-without-initialization-in-j
12.https://www.quora.com/How-does-JVM-allocate-memory-during-the-execution-of-the-
Java-program/answer/Aaryan-Verma-15
13.https://www.artima.com/insidejvm/ed2/jvm2.html read until page 3
14.http://i.stack.imgur.com/NDD6E.jpg not done
https://bit.ly/ps