This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes user defined data type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "user defined data type" Watch "user defined data type" New topic
Author

user defined data type

mudigonda manjunath
Greenhorn

Joined: Aug 26, 2008
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

But of course you can do this:



In Java, you create user defined data types by creating classes.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Aug 26, 2008
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Dec 22, 2004
Posts: 142

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?
 
Consider Paul's rocket mass heater.
 
subject: user defined data type
 
Similar Threads
explain
Confused
Mock exam question
Passing Arguments
getting to a 300 answer to compile