Basic Java Syntax
Types of variables in Java
* Local Variables * Class Variables (Static Variables) * Instance Variables (Non-static Variables)
public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } }
A class is a blueprint from which individual objects are created. Following is a sample of a class.
Declaration
A variable declaration with a variable name with an object type.
identifiers
All Java components require names. Names used for classes, variables, and methods are called ______________.
Method Names
All _________________ should start with a Lowercase letter. If several words are used to form the name of the method, then each inner word's first letter should be in Uppercase.
Class variables
Class variables are variables declared within a class, outside any method, with the static keyword.
Instance Variables
Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.
Class Names
For all ____________ the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
Case Sensitivity
Java is ______________, which means identifier Hello and hello would have different meaning in Java.
public static void main(String args[])
Java program processing starts from the main() method which is a mandatory part of every Java program.
Program File Name
Name of the program file should exactly match the class name.
Initialization
The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Instantiation
The 'new' keyword is used to create the object.
Message Parsing
To split a file or other input into pieces of data that can be easily stored or manipulated.
Notepad, Netbeans, Eclipse
To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market.
Platform Independent
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code.
Local variables
Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
escape character - Double quote
\"
escape character - backslash
\\
escape character - backspace
\b
escape character - form feed
\f
escape character - newline
\n
escape character - carriage return
\r
escape character - tab
\t
Encapsulation
a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
Inheritance
a mechanism wherein a new class is derived from an existing class.
Arrays
are objects that store multiple variables of the same type.
String literals
are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes.
Literals can be assigned to any primitive type variable. For example:
byte a = 68; char a = 'A'
Primitive Data Types
byte, short, int, long, float, double, boolean, char
Class
can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
String and char types of literals
can contain any Unicode characters.
example of String & char literals
char a = '\u0001'; String a = "\u0001";
Access Modifiers
default, public , protected, private
Non-access Modifiers
final, abstract, strictfp
Objects
have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class.
example of Java Literals for octal & hexa
int decimal = 100; int octal = 0144; int hexa = 0x64;
Method
is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
bytecode
is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
Polymorphism
is the ability of an object to take on many forms.
Abstraction
is used to hide certain details and only show the essential features of the object.
Prefix 0
is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals
/*..... */
mutli-line comment in Java
public class MyFirstJavaProgram { }
opening line of Java program
an example of a constructor
public class Puppy { public Puppy() { } public Puppy(String name) { // This constructor has one parameter, name. } }
an example of creating an object
public class Puppy { public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public static void main(String []args) { // Following statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } }
Enums
restrict a variable to have one of only a few predefined values.
Reference Datatypes
used to refer any object of the declared type or any compatible type.