Hi Friends,
As per my Knowledge, Following is the Tutorial on Java Programming Language for C and C++ Devlopers.
Hope this blog helps you a lot in finding useful Info to Learn Java with C/C++ Knowledge and It will be benefit you in day to day life.
Please provide me any feedback by your comments or any valuable suggestions.
Lets start with contents
Introduction
Classes & Objects
‘Package’ Concept
Inheritance
Exceptions
Collections
C++ concepts missing in Java
Java concepts missing in C++
Reference
Introduction to Java Language
- //A C main() function //A Java main() method
void main (int argc, char* argv[]) public static void main ( String[] args )
{ ………..} {…………..}
- Java guarantees the size, range of primitive types (same across all platforms)
- Primitive types in Java - boolean, char, byte, short, int, long, float, and double
- Java + operator concatenates String objects
- Labeled Breaks and Continues
- Java ‘final’ equal to const in c++
- C/C++ functions are called Java methods , can't define a method outside of class
- In Java, storage for the array itself is not allocated until you use "new”
int [] A; // A is a pointer to an array
A = new int [10]; // now A points to an array of length 10
A[0] = 5; // set the 1st element of the array pointed to by A
Inbuilt Array bound checking (java.lang.ArrayIndexOutOfBoundsException )
can determine the current length of an array using “.length”
- Java has String (read only) and StringBuffer (mutable string objects)
Memory Management & Garbage Collection
Memory Management
- Java completely removes the memory management load from the programmer
- Java has no pointers. Memory management model is based on objects and referenes to objects
- Objects are created with ‘new’, but there is no corresponding delete operation
Garbage collection
- Java uses ‘garbage collection’ to automatically frees blocks of memory sometime after all references to that memory have been redirected
- Garbage collection solve memory problems like ‘Dangling reference’, ‘Double deletion’, ‘Memory Leak’ etc.,
- Java has no pointers. Memory management model is based on objects and referenes to objects
- Objects are created with ‘new’, but there is no corresponding delete operation
Garbage collection
- Java uses ‘garbage collection’ to automatically frees blocks of memory sometime after all references to that memory have been redirected
- Garbage collection solve memory problems like ‘Dangling reference’, ‘Double deletion’, ‘Memory Leak’ etc.,
Execution Environments
C and C++ programs are compiled into machine language executables. An executable that runs on One machine will not run on another machine that uses a different machine
- All Java methods must be members of a class
- ‘this’ is a reference to the current object
- finalize( ), it gives you the ability to perform some important cleanup (special memory) at the time of garbage collection
C++: Java:
class Entity { class Entity {
protected: protected String name;
char *name; protected int x;
int x,y; protected int y;
public: public Entity() {
Entity(); x=y=0;
~Entity(); name=null;
void setName(char *inName); }
}; public void finalize() { .. } //Destructor
Entity::Entity() { public void setName(String name)
x=y=0; { … }
name=NULL; public void setXY(int inX, int iny)
} { … }
Entity::~Entity() { … } }
Entity::setName(char *iName) { … }
Java Modifiers
abstract class Contains unimplemented methods and cannot be instantiated.
method No body, only signature. The enclosing class is abstract
final class Cannot be sub classed
method Cannot be overridden
variable Cannot change its value.
public class Accessible anywhere
member Accessible anywhere its class is.
private member Accessible only in its class
protected member Accessible only within its package and its subclasses
none (package) class Accessible only in its package
member Accessible only in its package
static method A class method, invoked through the class name.
field class variable, invoked through the class name
synchronized method For a static method, a lock for the class is acquired before
executing the method.For a non-static method, a lock for the
specific object instance is acquired.
specific object instance is acquired.
Packages
- A Java package is a mechanism for grouping Java classes and ‘namespace management’.
- Packaging also help us to avoid class name collision when we use the same class name as that of others.
- To create a package
//in the Circle. java file
package graphics;
public class Circle extends Graphic implements Draggable { . . . }
//in the Rectangle. java file
package graphics;
public class Rectangle extends Graphic implements Draggable{ . . . }
public class Circle extends Graphic implements Draggable { . . . }
//in the Rectangle. java file
package graphics;
public class Rectangle extends Graphic implements Draggable{ . . . }
- How to use package
import world.*; // we can call any public classes inside the world package
import world.moon.*; // any public classes inside the world.moon package
import java.util.*; // import all public classes from java.util package
import java.util.Hashtable; // import only Hashtable class
import world.*; // we can call any public classes inside the world package
import world.moon.*; // any public classes inside the world.moon package
import java.util.*; // import all public classes from java.util package
import java.util.Hashtable; // import only Hashtable class
- Setting up the CLASSPATH
The Class path is an argument set on the command-line, or through an environment variable, that tells the Java Virtual Machine where to look for user-defined classes and packages in Java programs.
set CLASSPATH=path1;path2 ...
The Class path is an argument set on the command-line, or through an environment variable, that tells the Java Virtual Machine where to look for user-defined classes and packages in Java programs.
set CLASSPATH=path1;path2 ...
Core packages in Java
java.lang — basic language functionality and fundamental types
java.util — collection data structure classes, threads
java.io — file operations
java.math — multiprecision arithmetics
java.nio — the New I/O framework for Java
java.net — networking operations, sockets, DNS lookups, ...
java.sql — Java Database Connectivity (JDBC) to access databases
java.awt — basic hierarchy of packages for native GUI components
javax.swing — hierarchy of packages for platform-independent rich GUI components
java.applet — classes for creating and implementing applets
java.security— key generation, encryption and decryption
java.util — collection data structure classes, threads
java.io — file operations
java.math — multiprecision arithmetics
java.nio — the New I/O framework for Java
java.net — networking operations, sockets, DNS lookups, ...
java.sql — Java Database Connectivity (JDBC) to access databases
java.awt — basic hierarchy of packages for native GUI components
javax.swing — hierarchy of packages for platform-independent rich GUI components
java.applet — classes for creating and implementing applets
java.security— key generation, encryption and decryption
C++:
class Sub: public Base{
}
constructor ():SuperConstructor () { … }
Java:
class Sub extends Base{
}
super()
- super keyword to explicitly refer to superclass members from a subclass.
- All classes in Java ultimately inherit from the Object class. This is significantly different from C++ here it is possible to create inheritance trees that are completely unrelated to one another.
class Sub: public Base{
}
constructor ():SuperConstructor () { … }
Java:
class Sub extends Base{
}
super()
- super keyword to explicitly refer to superclass members from a subclass.
- All classes in Java ultimately inherit from the Object class. This is significantly different from C++ here it is possible to create inheritance trees that are completely unrelated to one another.
Interfaces
- interface is a group of related methods with empty bodies (pure virtual functions c++)
- Interfaces cannot be instantiated - they can only be implemented by classes or extended by other
interfaces
Interface Account {
Public static final float INTEREST=0.35F;
Public void with draw (float amount);
Public void deposit (float amount); }
Implementing interfaces
Interface Account {
Public static final float INTEREST=0.35F;
Public void with draw (float amount);
Public void deposit (float amount); }
Implementing interfaces
- A Java class can extend only one class, but it can implement any number of interfaces
- When a class implements an interface, it must implement every method defined in that
- When a class implements an interface, it must implement every method defined in that
interface
class SavingsAccount implements Account{
private float balance;
public void withdraw (float amount){ balance+=balance; }
public void deposit (float amount){ balance-=balance; } }
Abstract class
private float balance;
public void withdraw (float amount){ balance+=balance; }
public void deposit (float amount){ balance-=balance; } }
Abstract class
- An abstract class is a class that cannot be instantiated
- An abstract method is a method that is declared without an implementation (‘pure virtual
- An abstract method is a method that is declared without an implementation (‘pure virtual
function’ in c++)
- When an abstract class is subclassed, the subclass usually provides implementations for all of
- When an abstract class is subclassed, the subclass usually provides implementations for all of
// declare non-abstract methods
abstract void draw(); }
Inner classes and inner interfaces
- A class or interface that is defined inside an other class or interface is called an inner class or
inner interface
- Like Nested classes in C++
Exceptions
- Exceptions are objects to handle errors and other exceptional events
- Exceptions: throw, try, catch. Java adds finally
abstract void draw(); }
Inner classes and inner interfaces
- A class or interface that is defined inside an other class or interface is called an inner class or
inner interface
- Like Nested classes in C++
Exceptions
- Exceptions are objects to handle errors and other exceptional events
- Exceptions: throw, try, catch. Java adds finally
// In main()
try
{
fun();
}
catch (MyException1 e)
{ System.out.println(e); }
catch (Exception e) // catch any exception
{ …. }
finally {
// Release resources
}
// In fun()
throw new MyException ("text here");
How to define User defined Exceptions :
// In file except/ex1/TemperatureException.java
class TemperatureException extends Exception { .. }
// In file except/ex1/TooColdException.java
class TooColdException extends TemperatureException { .. }
// In file except/ex1/TooHotException.java
class TooHotException extends TemperatureException { .. }
Finally
- The finally block always executes when the try block exits
C++ concepts missing in Java
No Pointers
No preprocessor
No destructors
No Global variables, Global functions
No friend classes
No struct, union, typedef
No Operator Overloading
No templates
No Multiple Inheritance
Java concepts missing in C++
Garbage Collection
Packages (CLASSPATH)
Interfaces
Multithreading
Platform Independence
GUI development
Networking
JDBC (database connectivity)
Applets
try
{
fun();
}
catch (MyException1 e)
{ System.out.println(e); }
catch (Exception e) // catch any exception
{ …. }
finally {
// Release resources
}
// In fun()
throw new MyException ("text here");
How to define User defined Exceptions :
// In file except/ex1/TemperatureException.java
class TemperatureException extends Exception { .. }
// In file except/ex1/TooColdException.java
class TooColdException extends TemperatureException { .. }
// In file except/ex1/TooHotException.java
class TooHotException extends TemperatureException { .. }
Finally
- The finally block always executes when the try block exits
- The finally block is a key tool for preventing ‘resource leaks’
- Release resources irrespective of exceptions like file handles, sockets, database connections etc
C++ concepts missing in Java
No Pointers
No preprocessor
No destructors
No Global variables, Global functions
No friend classes
No struct, union, typedef
No Operator Overloading
No templates
No Multiple Inheritance
Java concepts missing in C++
Garbage Collection
Packages (CLASSPATH)
Interfaces
Multithreading
Platform Independence
GUI development
Networking
JDBC (database connectivity)
Applets
References
‘The Java Programming Language’ by James Gosling's
http://java.sun.com/docs/books/tutorial/java/index.html
http://www.ibm.com/developerworks/edu/j-dw-ijcc++p-i.html
‘The Java Programming Language’ by James Gosling's
http://java.sun.com/docs/books/tutorial/java/index.html
http://www.ibm.com/developerworks/edu/j-dw-ijcc++p-i.html
About the Author :
I, Author is currently working as Senior Programmer at CISCO Systems , India.
I Pursued M.Tech degree in Computer Science from V.T.U, Bangalore and B.Tech in
Computer Science from Nagarjuna University.
My Hobbies are Coding , Watching TV, Listening to music.
I, Author is currently working as Senior Programmer at CISCO Systems , India.
I Pursued M.Tech degree in Computer Science from V.T.U, Bangalore and B.Tech in
Computer Science from Nagarjuna University.
My Hobbies are Coding , Watching TV, Listening to music.
Reach me at naresh.master3@gmail.com