Java tutorial 3

 

C++ vs Java

The Java programming language and C++ have many points in common and in contrast. The following is a list of the key distinctions between C++ and Java:


Index of Comparison

C++

Java

 

 

Platform-independent

Platforms are necessary for C++.

Java works across all platforms.

 

generally used for

The majority of system programming is done in C++.

The primary usage of Java is application programming. It is commonly used in web-based, business, mobile, and Windows-based applications.

 

Design Objective

Programming systems and applications is the purpose of C++. It was a C programming language extension.

Java was initially developed as a printing system interpreter and then expanded to accommodate network computing. It was created to be user-friendly and open to a larger audience.

 

Goto

The goto statement is available in C++.

Goto statement functionality is not available in Java.

 

numerous inheritance

The multiple inheritance feature of C++.

Java does not support class-based multiple inheritance. Java interfaces may be used to do that.

 

overloading the operator

In C++, operator overloading is supported.

Overloading of operators is not supported in Java.

 

Pointers

There are pointers in C++. In C++, you may create pointer programmes.

Internally, Java supports pointers. The pointer programme, however, cannot be created in Java. It implies that Java only supports certain types of pointers.

 

Creator and Interpreter

C++ is the sole compiler. C++ is platform-dependent since it is built and executed using the compiler, which transforms source code into machine code.

Ava employs both an interpreter and a compiler. At the moment of compilation, bytecode is created from Java source code. This bytecode is executed at runtime by the interpreter, which also generates output. Java is interpreted, making it independent of operating systems.

 

Call by Reference and Call by Value

Both call by value and call by reference are supported in C++.

Java only supports calls with values. Java does not support calls by reference.

 

Organization and Union

Unions and structures are supported in C++.

Structures and unions are not supported by Java.

 

Support for Thread

There is no built-in thread support in C++. In order to support threads, it uses third-party libraries.

Java provides built-in support for threads.

 

Note regarding the documentation

Comments in documentation are not supported by C++.

To write documentation for Java source code, Java offers documentation comment (/**... *).

 

Vulnerable Keyword

The virtual keyword is supported by C++, allowing us to choose whether or not to override a function.

No virtual keywords exist in Java. By default, we are able to override all non-static methods. So, by default, non-static methods are virtual.

 

unauthorized right shift >>>

The >>> operator is not available in C++.

 

 

Java has an unsigned right shift operator that replaces the top of the array with zero for negative integers. It functions the same way as the >> operator for positive values.

Tree of Inheritance

A fresh inheritance tree is always created by C++.

Because all classes in Java are descendants of the Object class, Java only ever utilises one inheritance tree. In Java, the inheritance tree begins with the Object class.

Hardware

Nearer to hardware is C++.

 

 

Java doesn't interface with hardware as much.

Object-oriented

An object-oriented language is C++. However, a single root hierarchy is not feasible in the C programming language.

Another object-oriented language is Java. In Java, however, everything (apart from fundamental types) is an object. Due to the fact that everything derives from java.lang, there is only one root hierarchy. Object.



Note

  • Unlike C++, Java does not provide default parameters.
  • Unlike C++, Java does not allow header files. The import keyword in Java is used to incorporate various classes and functions.

Program Example in C++

(main.cpp)

#include <iostream>  
using namespace std;  
int main() 
{  
   cout << "Hello C++ Programming";  
   return 0;  
}  

#include <iostream>  
using namespace std;  
int main() 
{  
   cout << "Hello C++ Programming";  
   return 0;  
}  
#include <iostream>  
using namespace std;  
int main() 
{  
   cout << "Hello C++ Programming";  
   return 0;  
}  

Output:

Hello C++ Programming

Program Example in Java

(Simple.java)

class Simple{  
    public static void main(String args[])
{  
     System.out.println("Hello Java");  
    }  
}  

Output:

Hello Java

first Java programme |Examples of Hello World

We will learn how to create a basic Java programme in this part. After installing the JDK, it's straightforward to construct a Java application that says "hello."

You must develop a class that has the main function in order to create a straightforward Java application. Let's start by comprehending the prerequisite.

The requirement for Java Hello World Example

The following software or application has to be correctly installed in order to run any Java programme.

  • Setup the JDK Download and install the JDK if you haven't already.
  • Set the jdk/bin directory's path. set path=C:\Program Files\Java\jdk1.6.0_23\bin.
  • Build the Java application.
  • Run and compile the Java application.

Creating Hello World Example

Let's create the hello java program:

class Simple{  
    public static void main(String args[])
{  
     System.out.println("Hello Java");  
    }  
}  

Output:

Hello Java


Flow of Compilation:

The Java compiler creates byte code from the source code when we compile a Java application using the javac tool.

                Simple.java                                                                   Simple.code                      

Variables in the First Java Program

See what class, public, static, void, main, String[,] and System.out.println imply ().

  • In Java, a class is declared with the class keyword.
  • An access modifier that denotes visibility is the term public. It means that everyone can see it.
  • A keyword is static. Any method that has been declared static is referred to as a static method. The main benefit of using a static method is that you don't need to build an object in order to use it. It is not necessary to create an object in order to call the main() function because the JVM runs it. This preserves memory.
  • The method's return type is void. It indicates that it gives no value back.
  • The program's entry point is represented by the main function.
  • For command line arguments, use String[] args or String[]. We'll talk about it in the part after this.
  • Statements are printed using System.out.println(). In this case, println() is a method and out is an object of the PrintStream class. System is also a class. In the next part, we'll go over how the System.out.println() command internally functions.
How many different ways can a Java application be written?
A Java programme can be written in a variety of ways. The following list includes the modifications that can be made to a Java programme:

1) In Java, the method prototype cannot be modified by rearranging the modifiers.

Let's look at the main method's straightforward code.

main static public void (String args[])
2) The Java array's subscript notation can be applied before, after, or after the variable.

Let's examine the many codes used to create the main method.

void public static main (String[] args)
void public static main (String []args)
void public static main (String args[])
3) You may support var-args by giving three ellipses to the main() function (dots)
Let's look at the straightforward code for the main() method's use of var-args. Var-args will be covered in more detail in the chapter on Java New Features.


void public static main (String... args)

4) In Java, the use of a semicolon at the end of a class is optional.



Let's look at the basic code.

class A
{  
static public void main(String... args)
{  
System.out.println("hello java4");  
}  
};  

Valid Java main() method signature

public static void main(String[] args)  
public static void main(String []args)  
public static void main(String args[])  
public static void main(String... args)  
static public void main(String[] args)  
public static final void main(String[] args)  
final public static void main(String[] args)  
final strictfp public static void main(String[] args)  

Invalid Java main() method signature

public void main(String[] args) static void main(String[] args) public void static main(String[] args) abstract public static void main(String[] args)

How can I fix the "javac is not recognised as an internal or external command" error?

You must specify a path if an issue arises like that shown in the image below.
Javac and Java are not recognised as internal or external commands by DOS.
We must create a plan in order to solve this issue.
If you save your programme under the JDK/bin directory, the path is not necessary.
However, establishing the road is a great strategy.
To learn how to set a route in Java, go here.




Comments

Popular posts from this blog

Java tutorial 2