Real Differences Between "Java -Server" and "Java -Client"

what is the difference between `| `and` || `in java?

a || b never evaluates b if a evaluates to true.

Difference between using javac<file> and java<file>

They're equivalent if your source code is only a single file. The former (with the two commands) is the general way to compile and run Java source code, and it's still the correct way to compile larger projects. The latter is a new feature added in JDK 11 to make it easier to run individual files and very small programs.

From the proposal that suggested the feature

In source-file mode, the effect is as if the source file is compiled
into memory, and the first class found in the source file is executed.
For example, if a file called HelloWorld.java contains a class called
hello.World, then the command

java HelloWorld.java

is informally equivalent to

javac -d <memory> HelloWorld.java
java -cp <memory> hello.World

Why do we usually use || over |? What is the difference?

If you use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand alone.

It's a matter of if you want to short-circuit the evaluation or not -- most of the time you want to.

A good way to illustrate the benefits of short-circuiting would be to consider the following example.

Boolean b = true;
if(b || foo.timeConsumingCall())
{
//we entered without calling timeConsumingCall()
}

Another benefit, as Jeremy and Peter mentioned, for short-circuiting is the null reference check:

if(string != null && string.isEmpty())
{
//we check for string being null before calling isEmpty()
}

more info

why c is preferred instead of java in most of the real time application

There are a number of reasons:

  1. History - the airline system is older than Java. It might need a rewrite, but it's not in progress today that I know of.
  2. Real time generally steers clear of garbage collection, because you can't have the system waiting at a delicate juncture for the GC thread to finish its work. Things have to be more deterministic in real time control situations. This would be true of Java, C#, and any other language that uses GC.

There is a real time version of Java, but I don't know how widely it's used.

I'm not sure that the conclusion of C/C++ always being faster than Java is still true for JDK 6. A lot has changed since the 1.0 version when a lot of the benchmarks were performed (e.g., faster object creation, new memory model, new generational GC algorithms, revised reflection, etc.).

What is the difference between Java subtype and true subtype

A java subtype is any class that extends another class (or even implements an interface). A true subtype is not something language specific:

A true subtype can always be substituted for the supertype.
"Any property guaranteed by supertype must be guaranteed by subtype (true subtyping)"

http://www.cs.washington.edu/education/courses/cse331/10au/lectures/subtypingexamples.pdf

The link contains a very enlightning example. Let's say you have a class Point2D that stores values x and y. You could now create a subtype Point3D and add a value z. If you don't override any methods and take care with your equals and hashcode methods you could substitute a Point3D instance at any time for a Point2D instance.

This is a simple example of course. One could argue why not have only Point3D. Maybe the classes both offer some methods that can - by dividing up into to classes - be better recognized as belonging to the 2D or 3D realm. In this case it would probably be purely a design decision.

class Point2D {
int x;
int y;
}

//true subtype
class Point3D extends Point2D {
int z;
}

A more complex example might arise if you take a Person class and then have two subtypes: Employee and Customer. Both Employee and Customer offer all the same fields and methods as a Person.

class Person {
String name;
Date birthday;

@Override
public boolean equals(Object o){
//simplified equals implementation, this does not fulfill equals contract!
return name.equals(((Person)o).name);
}
}

//true subtype, same behaviour
class Employee extends Person {
long departmentId;
}

//not a true subtype, different behaviour -> equals
class Customer extends Person {
long customerId;
Date lastContact;
String city;

public boolean equals(Object o){
//simplified equals implementation, this does not fulfill equals contract!
return customerId.equals(((Customer)o).customerId);
}
}

In this example Employee would be a true subtype of Person. However Customer is not a true subtype because the equals implementation differs (and presumably hashCode() as well) and it would not behave the same and could probably not be substituted for a Person object at all times.

Difference between .java and .txt files

There can be many text file formats, just as there are many binary file formats. Using separate extensions makes them easy to differentiate, especially for the operating system (display icon, open with, etc.).

What are the key differences between Java 8's Optional, Scala's Option and Haskell's Maybe?

Some possibilities come to mind (OTOH, I haven't seen people actually saying that, so they might mean something else):

  1. No pattern matching.

  2. No equivalent to Scala's fold or Haskell's fromMaybe: you have to do optional.map(...).orElseGet(...) instead.

  3. No monadic syntax.

I also wouldn't call any of these "less powerful" myself, since you can express everything you can with the corresponding Scala/Haskell types; these are all conciseness/usability concerns.

How to tell the difference between Java and Kotlin code?

some obvious traits for kotlin:

  • as already mentioned the most obvious is semicolons, although you can
    add them and they will just be ignored

  • variables marked with question marks (nullability) val foo:String? and the usage of val/var/lateinit

  • class Foo : Something() <-- this is inheritance, there's no extends
    or implements

  • if files are involved, kotlin files end with .kt

  • any reference to companion object


personally for me the biggest indicator:

fun :) function declaration :

fun foo(): String


Related Topics



Leave a reply



Submit