Showing posts with label Java SE. Show all posts
Showing posts with label Java SE. Show all posts
Java SE from sketch - Part 2
Java Primitive Data Types:
Data types are to define various types of data in the program to be used, there are 8 primitive data types in java.

Java Variable or Identifier:- Variables are to hold different type of data to be used in the program
- Java is strict case sensitive language
- Identifiers must start with a letter, a currency character ($), or a connecting
- character such as the underscore ( _ ). Identifiers cannot start with a number!
- After the first character, identifiers can contain any combination of letters,currency characters, connecting characters, or numbers.
- Key words should be ignored to be an identifier
int _a;
int $c;
int ______2_w;
int _$;
int this_is_a_very_detailed_name_for_an_identifier;
Illegal Identifiers:
int :b;
int -d;
int e#;
int .f;
int 7g;
Java Code conventions:
Sun defined a coding standard and style programmer. Here are the naming standards that Sun recommends as follow,
Classes:
The first letter should be capitalized, and if several words are linked together to form the name, both the words should have first letter uppercases. Classes, the names should typically be nouns like,
System
Account
StudentProfile
Interfaces:
The names should be adjectives like,
Runnable
Serializable
Methods:
The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs like,
getName ()
setFullName()
validateUser()
Variables:
Like methods, the camelCase format should be used, starting with
a lowercase letter like,
maxWidth
accountBalance
formName
Constants:
They should be named using uppercase letters with underscore characters as separators like,
YEAR_RATE
FINAL_VALUE
Writing First Java Program:
Being an OOP language each Java Program has to be written with a class. Let us write a simple program to begin,
public class MyFirst
{
public static void main(String[] args)
{
System.out.println("New Line");
System.out.print("After New Line");
System.out.println("Not in new Line");
}
}
output:
New Line
After New LineNot in new Line
Here we created a public class called MyFirst having a main method,
public static void main(String[] args) {
}
This main method happens to be the starting point of the program and also all other methods will be called from this method.
System.out.println("New Line");
Here System is a built-in class, out is an Object of output Stream and println(); is the method to display data in the console.
Println(); prints data in a line and moves the cursor to new line. Please remember it never starts in a new line.
Print(); print along with current line.
Output of the program should give the complete idea.
NB: about public, static, void and String[] we will discuss soon.
to be continued...
- Labels: Java SE
- (0) Comments
Java SE from sketch - Part 1
History at a glance:
- In the year around 1990James Gosling along with 13 team members started planning for of computing and formed a team called Green Team.
- Their primary goal was to control over handheld devices like cable set-top boxes,VCR's, toasters, *7-type of device, and also for personal data assistants (PDA).
- They came up with a language named OAK with having features with Platform independent, Extremely reliable, Compact.
- Internet and Web explosion began, so Sun shifted the target market to Internet applications and changed the name of the project OAK to Java.
- Having passed a bad time since 1991 – 1993, In 1994 Sun's HotJava browser appeared.
- HotJava illustrated the power of applets, programs that run within a browser, and also the capabilities of Java for speeding program development.
- In 1995, Netscape Navigator Internet browser incorporated Java technology officially.
- And Java alpha version Java1.0a2 introduced in the market.
- Since then now we have Java 6 with 12 update so far
What is Java:
Java is a OOP language which refers with several others language like C, C++ or Pascal having different new perception as
Java has incorporated plenty of exiting features to buzz the programming myth, some of are
Plaform Independent vs Architecture Neutral:
PI refers to running the programs written in one platform (Operating System) will be the same for other platform. Software specific
AN refers to writing a program for a Machine (e.g. Pentium) will be the same for any other machine architecture. Hardware specific
Java is a OOP language which refers with several others language like C, C++ or Pascal having different new perception as
- Pure OOP language
- Java byte code
- JVM (Java Virtual Machine)
- JRE (Java Runtime Environment)
Java has incorporated plenty of exiting features to buzz the programming myth, some of are
- Platform Independency
- Compiler/Interpreter Pack
- Automatic Memory Management
- Architecture neutrality
- Security
Plaform Independent vs Architecture Neutral:
PI refers to running the programs written in one platform (Operating System) will be the same for other platform. Software specific
AN refers to writing a program for a Machine (e.g. Pentium) will be the same for any other machine architecture. Hardware specific
Byte Code and Binary Machine Code:
Byte code is alternative form of Machine code which is understood only by the JVM generated by java compiler orally known as .class file. When JVM loads a .class file in fact it loads stream of byte code for each method there in the class. And finally execute by java interpreter, JIT (just-in-time) or JVM specific techniques.
Binary code falls a hidden mechanism here in Java where JVM act smart o generate neutral machine code against all Byte Code stream taken from a class file. Mr. Cute JVM act as a Microprocessor here!!!
JVM as Microprocessor:
As name says JVM is a Virtual Machine which has its own instruction sets as Microprocessors does. Each instruction consists of a one-byte opcode followed by zero or more operands. The opcode indicates the action to be taken. If more information is required before the JVM can take the action, that information is encoded into one or more operands that immediately follow the opcode. For each kind of data to be handled either logically or arithmetically JVM does with its own separate instruction a set.
Sample instruction opcodes of JVM :
iconst_m1, iconst_0 , iconst_1 , iconst_1
Sample Byte code Stream :
03 3b 84 00 01 1a 05 68 3b a7 ff f9
Sample Machine code by JVM:
iconst_0 = 03
istore_0 = 3b
iinc 0, 1 = 84 00 01
iload_0 =1a
iconst_2 = 05
imul = 68
istore_0 = 3b
goto -7 = a7 ff f9
Finally to say Java is smart because it has JVM. All praises Java but JVM the hero. Sorry to say JVM not Platform independent because each platform needs a separate JVM.
Byte code is alternative form of Machine code which is understood only by the JVM generated by java compiler orally known as .class file. When JVM loads a .class file in fact it loads stream of byte code for each method there in the class. And finally execute by java interpreter, JIT (just-in-time) or JVM specific techniques.
Binary code falls a hidden mechanism here in Java where JVM act smart o generate neutral machine code against all Byte Code stream taken from a class file. Mr. Cute JVM act as a Microprocessor here!!!
JVM as Microprocessor:
As name says JVM is a Virtual Machine which has its own instruction sets as Microprocessors does. Each instruction consists of a one-byte opcode followed by zero or more operands. The opcode indicates the action to be taken. If more information is required before the JVM can take the action, that information is encoded into one or more operands that immediately follow the opcode. For each kind of data to be handled either logically or arithmetically JVM does with its own separate instruction a set.
Sample instruction opcodes of JVM :
iconst_m1, iconst_0 , iconst_1 , iconst_1
Sample Byte code Stream :
03 3b 84 00 01 1a 05 68 3b a7 ff f9
Sample Machine code by JVM:
iconst_0 = 03
istore_0 = 3b
iinc 0, 1 = 84 00 01
iload_0 =1a
iconst_2 = 05
imul = 68
istore_0 = 3b
goto -7 = a7 ff f9
Finally to say Java is smart because it has JVM. All praises Java but JVM the hero. Sorry to say JVM not Platform independent because each platform needs a separate JVM.
JDK vs JRE vs JVM:
JDK: Java Development Kit holds all the tools needed to develop the Java programs. These tools are compiler i.e. javac, Java application launcher i.e.java.exe, Appletviewer, etc. If wejust needed to write a Java program and compile then JDK is enough.
JRE: Java Runtime Environment contains JVM, class libraries, and other supporting files. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE.
JVM: Java Virtual Machine interprets the bytecode into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc.
Java Exceution processes:
Compiling (JDK part):
Executing (JRE part):
- JDK to compile Java Program and generate bytecode
- JRE to run the Java Program with the help of JVM
- JVM to interpret Java bytecode to machine code for low level execution.
JDK: Java Development Kit holds all the tools needed to develop the Java programs. These tools are compiler i.e. javac, Java application launcher i.e.java.exe, Appletviewer, etc. If wejust needed to write a Java program and compile then JDK is enough.
JRE: Java Runtime Environment contains JVM, class libraries, and other supporting files. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE.
JVM: Java Virtual Machine interprets the bytecode into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc.
Java Exceution processes:
Compiling (JDK part):
My.java--->javac My.java (Compiling)--->My.class (Bytecode)
Executing (JRE part):
java My (Interpreting)--->JVM---> Machine Code--->Microprocessor---> Output
- Labels: Java SE
- (0) Comments
Java Best Practice.
It’s been a long time we have been moving with a traditional way of Java programming which makes the programs tightly coupled and also leads the program a hassled one to be modified or to make generic.
Let us see a traditional program followed by a modern programming techniques to make programs more generic and expendable… we also call it best practice of Java programming in oppose to traditional style of programming.
Traditional:
package traditional;
public class Traditional {
public static void main(String[] args) {
System.out.println("Traditional Programming!!!");
}
}
Modern:
package modern;
import java.io.PrintStream;
final class Source {
private final String message;
public Source(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
class Destination {
public void write(PrintStream out, String message) {
out.println(message);
}
}
public class Mordern {
public static void main(String[] args) {
Source source = new Source("Modern Programming!!!");
Destination destination = new Destination();
destination.write(System.out, source.getMessage());
}
}
- Labels: Java SE
- (0) Comments
Subscribe to:
Posts (Atom)