Just sharing about Java, or maybe it's not...

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Java:
public class Test {
  static class Individual {
    private int cnt;


    Individual() {
      this.cnt = 1;
    }


    Individual(int cnt) {
      this.cnt = cnt;
    }


    public String toString() {
      if (cnt < 0)
        return "zzz...";
      return switch(cnt) {
        case 0  -> "Knock knock..";
        case 1  -> "I am an individual";
        case 2  -> "We are a couple";
        case 3  -> "This is a trio";
        case 4  -> "This is a foursome";
        default -> "We are a group";
      };
    }


    public Individual plus(Individual other) {
      return new Individual(cnt + other.cnt);
    }


    public Individual plus(int otherCnt) {
      return new Individual(cnt + otherCnt);
    }


    public Individual inc() {
      cnt++;
      return this;
    }


    public Individual minus(Individual other) {
      return new Individual(cnt - other.cnt);
    }


    public Individual minus(int otherCnt) {
      return new Individual(cnt - otherCnt);
    }


    public Individual dec() {
      cnt--;
      return this;
    }
  }

  public static void main(String[] args) {
    Individual i = new Individual();
    Individual j = new Individual();
    Individual k = i + j;
    System.out.println("I: " + i);
    System.out.println("J: " + j);
    System.out.println("K: " + k);
    k = k + 1;
    System.out.println("K(Again): " + k);
    k++;
    System.out.println("K(Yet again): " + k);
    k = 10 + k;
    System.out.println("K(Yet again x2): " + k);
    k = k - k;
    System.out.println("K(Zero out): " + k);
    k--;
    System.out.println("K(Eternal): " + k);
  }
}

Bash:
$ ./compile.sh && ./run.sh
I: I am an individual
J: I am an individual
K: We are a couple
K(Again): This is a trio
K(Yet again): This is a foursome
K(Yet again x2): We are a group
K(Zero out): Knock knock..
K(Eternal): zzz...

What do you think ?
:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Only read code, never compile, cos my comp spoil

In that case, try again what you have a comp.

The first paragraph describing what is auto boxing/unboxing would have not qualify what I have provided.

If you really knows Java well, you can easily find out what is wrong without using a compiler or IDE.

Also think about why you are even directed from other thread to read this. What did I reply to?
:)
 

Clarence NMT

Supremacy Member
Joined
Dec 28, 2012
Messages
8,391
Reaction score
2,789
In that case, try again what you have a comp.

The first paragraph describing what is auto boxing/unboxing would have not qualify what I have provided.

If you really knows Java well, you can easily find out what is wrong without using a compiler or IDE.

Also think about why you are even directed from other thread to read this. What did I reply to?
:)
Idk what is about, operator overload? Or maybe inner static class?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
U mean got something not java? Btw java what version?

Java no operator overload like c++ lor.

Btw like ur return switch and java8 tenary not like c++ too.

If the above is somewhat Java and looks very much like Java, did you see operator overloading behaviour?

The above uses javac on version 17. You use version 13 would be fine too.

:)
 

Clarence NMT

Supremacy Member
Joined
Dec 28, 2012
Messages
8,391
Reaction score
2,789
= , + , -, post ++ & -- ?, but u making wrapper class and the base is still primetive just use Integer can liao
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top