Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

user defined data type

 
mudigonda manjunath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiii am working on my final Project n looking for some thhing like this

ex: int x=10 , y = 20;
system.out.println(x+y);

returns 30;

i wana use int as xyz(say)

so the program will be like this

xyz x =10 , y= 20;
System.out.println(x+y);

returns 30

note : here xyz represents int

how to ... do this kindly help me out ASAP thanks in adv

manju_mca17@yahoo.com
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't.

In Java, only a predefined set of types can be initialized using constants: String, primitives and since Java 5.0 the primitive wrappers (Integer, Boolean, etc). All others are NOT possible.


Now I know it was possible in C (using typedefs), and Java is based on C and C++, but typedefs usually only make it harder to read code, and therefore it was omitted in Java.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But of course you can do this:



In Java, you create user defined data types by creating classes.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that removes the initialization with primitive or String literals, and the use of standard Java operators.

It's the only way to do it in Java though, and Sun have noticed this themselves too. There are two classes called BigDecimal and BigInteger. These make use of methods for simple arithmetic such as addition, subtraction and multiplication. Sun have finally seen this is bothersome so they will probably change Java in such a way that, much like String, these classes too will work with arithmetic operators.

So instead of

you will be able to write


However, Sun must be willing to do this, and for 99.999% of the classes, they simply won't.
 
mudigonda manjunath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx ....

i did the normal class projection view for ... the above example. It was my prof. striclty on the same techinical direction. So i need to find sol. which i similar to the same. can we use <t> or <type> or ADT to get similar View.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseRealWords. I could figure out the prof. immediately, but had to think about sol. for a second to find out what it meant.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

However, Sun must be willing to do this, and for 99.999% of the classes, they simply won't.


Guess it's good they won't ... operator overloading is a powerful thing in C/C++, but also a fluent source of errors ... I further guess that beside the pooling the special function of the '+' operator in combination with Strings is one of the reasons that (a) Strings are immutable and (b) the String class is final.

Imagine what would happen if Java would allow to define operators for (pairs of) non-final classes, or even for interfaces ... this would easily result in conflicts, and in uncontrollable (though not totally unpredictable) behavior and side effects of changes to code. Imagine implementing one more interface with a later version of a class.

Wasn't the original intention of Java to be as use- and powerful as C++ (in the application domain, not for writing operating systems) while omitting the features of C/C++ that were the most error prone and made code hard to read and even harder to maintain?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic