A glance Java vs Go : What Java Offers That Go Does Not or alternatives in Go

There are many obvious and subtle differences between Java and Go. They can be compared as a language and as a runtime. Go is a powerful language, and it holds its own against Java. But Java does have features Go does not, and it will be summarized here. 

Note that many of the Go “missing” features were omitted deliberately to keep the language simple and efficient.

Multiple Assignments

Java can assign multiple variables to the same value in a single statement. For example:

    int x, y, z;
    x = y = z = 10;

In Go, the assigned types and values can be different.

	var x, y, z int = 10, 10, 10

Statements and Operators

Go and Java operators have different precedence. A key difference is that in Go x++ (means: x = x + 1) and x-- (means: x = x - 1) are statements, not operators. And there is no --x or ++x expressions at all.

Go does not support Ternary Expressions . The use of the if/else statement is required. For example, the Java expression, which gets the larger value.

In Java 
    var z = x > y ? x : y;

  In Go:
    var z = y
    if x > y {
       z = x
    }
or
    var z int
    if x > y { z =  }
    else { z = }

Go does not support assignment expressions, only assignment statements.

While and Do Statements

The Java while statement is replaced by the Go for statement (i.e., for acts like while). There is no direct equivalent of the Java do statement, but the for statement can be used to replace it.
Go for loop used as while/or infinite loop.
    for initialization; condition; post {
        // zero or more statements
    }
    // a traditional "while" loop
    for condition {
        // ...
    }

 An infinite loop with no condition
    // a traditional infinite loop
    for {
        // ...
    }

Objects and Classes (OOP)

Go does not fully support Object-Oriented Programming (OOP) as Java does. As a result, it does not support these Java constructs. Go has features described later in this text that can be used similarly to most of these OOP features. As such, Go is better described as an object-based language. Go does allow one to achieve the key goals of OOP, but in different ways than a strictly OOP language normally would do them.

Go does not support true Classes (i.e., the Java class declaration). Go does support Structs which are like classes but without inheritance. Go does allow nested structs, which are somewhat like inner classes.

Go has no extends or implements clauses on type declarations. Go has no inheritance as these clauses provide. Go does have an implied form of implements for its interface types.

Go does not support Java Lambdas (function signatures compiled into class instances). Instead, Go supports first-class functions (often literals) that may be passed as parameters. Go does not support Method References (simple names for lambdas passed as parameters).

Go supports interfaces in a different way than Java. Go’s interfaces allow Duck Typing . Go’s interfaces do not require that they be implemented explicitly (no implements clause is needed in Go); any type that has methods that match all the methods of the interface implicitly implements the interface. In general, Go’s approach is more flexible.

Java 8 and beyond allows implemented (concrete) methods in interfaces. Go does not. Java allows constants to be declared in interfaces; Go does not. Java allows subtypes to be declared in interfaces. Go does not.

Go’s lack of full OOP is probably its greatest shortcoming.OOP is not required to achieve high levels of reuse. Functions can fill that role well, especially if they are first class as they are in Go.

Generic Types and Methods

Go currently does not support Generic types and methods on arbitrary types. Here, generic means being able to hold/use multiple types. In Java, a variable of the Object type is generic in that it can hold a value of any reference type. In Go, a variable of the interface{} type is generic in that it can hold a value of any type.

Boxing of Primitive Values

Java collections (arrays excepted) cannot contain primitive values, only Objects. So, Java provides wrapper types for each primitive type. To make collections easier to use, Java will automatically wrap (box) a primitive into a wrapper type to insert it into a collection and unwrap (unbox) that value when taking it from a collection. Go supports collections that can hold primitives, so such boxing is not needed. Note the need to use boxing is an area where Java is less efficient at memory use than Go is.

Source Annotations

Go has no Annotations . Go Struct fields can have tags , which serve a similar but much more limited role.Annotations, along with function streams and lambdas, make Java (at least partially) a declarative language. Go is almost purely an imperative language. This is by choice. This tends to make Go code both more obvious and more verbose


Multiple Visibilities

Java supports four visibilities:
1. Private – Only the code in the containing type can see it.
2.     Default – Only the code in the same package can see it. 
3. Protected – Only the code in the same package or a subclass of the type can see it.
4. Public – Any code can see it.

 
Go supports only the equivalent of default (often called private or package in Go) and public visibilities. Gophers often refer to public visibility as “exported visibility” and private visibility as un-exported visibility.

Overloaded/Overridden Functions

In Java, functions with the same name but with different signatures (a different number of and/or types of arguments) can be defined in the same scope. These are called (via a form of parametric polymorphism) overloaded functions. Go does not allow overloading.

In Java, functions with the same name and signature can be redefined lower in the inheritance hierarchy. Such redefined functions are called (via inheritance polymorphism) overridden. As Go does not support inheritance, such overriding is not allowed.


Built-in Binary Data Self-Serialization

Java offers the ability to serialize data and objects in a binary form. Serialized data is often written into files or transmitted across networks and sometimes stored in databases. Serialization can offer a form of persistence to otherwise ephemeral objects. Serialization is also fundamental to most Remote Procedure Call (RPC) mechanisms. 

Java supports serialization of primitive values, arrays, and any data structure (class instance) containing primitive types or any type marked with the Serializable interface and any collections of these types. Java even supports structures with reference loops.

Go offers no direct equivalent to this full object serialization. Often in Go one serializes data to some text format (say JSON or XML) and saves/sends that form instead. Using text is often less efficient (more bytes and time needed) than a binary representation. These text forms generally do not support reference loops inside the data structure.

Go offers community support, such as Google Protocol Buffers, for binary data. With standard Go libraries, one can, somewhat tediously, create custom binary formats.


Go’s Runtime Is Simpler Than the JRE

Go has a much smaller runtime than that provided by the JRE. There is no JVM equivalent, but similar components, such as a garbage collection, exist in both. Go has no bytecode interpreter.

Go has a large set of standard libraries. The Go community provides many more. But the Java standard and community libraries arguably far exceed the current Go libraries in both breadth and depth of function. Still the Go libraries are rich enough to develop many useful applications, especially application servers.


Go Is a Compiled (vs. Interpreted, As Java Is) Language

Like C and C++, Go programs are completely built before execution begins. All source is compiled into the machine language of the target computer architecture.In contrast, Java is compiled into a virtual machine language (aka bytecode), and that is interpreted by the Java Virtual Machine (JVM ).

For improved performance, that bytecode is often compiled into machine language dynamically at runtime. The JVM itself is built for a particular operating system and hardware architecture.



Reference: Go for Java Programmers: Learn the Google Go Programming Language by Barry Feigenbaum,

5 Comments

  1. You can find for a quick essay writers on the site https://onehouressays.com , that can deliver high-quality results. This site offers a full range of essay types, from simple compositions to more complex research papers and dissertations. It also provides editing and proofreading services as well as a variety of formatting options, such as MLA, APA, and Chicago. Moreover, it has one of the best customer support teams in the business, with live chats available on its website and through Facebook Messenger.

    ReplyDelete
  2. สล็อต pg slot ใหม่ล่าสุด พวกเราในฐานะ คาสิโนออนไลน์ pgslot ผู้ให้บริการสล็อตรวมทุกค่ายผู้ให้บริการ สล็อตออนไลน์ที่ ยอดเยี่ยม เกมทำเงินผ่าน มือถือ ที่ทำเงินก้าวหน้า ที่สุด

    ReplyDelete
  3. เล่น pg slot cash มีบริการออนไลน์ที่คุณสามารถเล่น PG SLOT CASH ได้ ในปัจจุบันมีเว็บไซต์หลายแห่งที่ให้บริการเกมสล็อตออนไลน์จากค่าย PG SLOT ที่น่าสนใจ

    ReplyDelete
  4. Share great information about your blog , Blog really helpful for us .
    Plus Size Women's Clothing

    ReplyDelete
  5. Great Blog! HASHCRON Technologies Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today’s digital world, by providing the reliable platform upon which many services and applications are built. New, innovative products and digital services designed for the future continue to rely on Java, as well.

    ReplyDelete
Previous Post Next Post