File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Positive integer data type in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Positive integer data type in java" Watch "Positive integer data type in java" New topic
Author

Positive integer data type in java

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Hi,

I want to make a data type in java so that only positive integers can come in that data type.

It means if we try to assign -4 to it, it should not accept that but 4(positive) should be accepted.

Can anybody tell if it is possible in java?. If yes, how?

-Raj
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Sure that is possible.

What have you tried so far?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And please UseRealWords. +ve is not even a word and only confuses people. I'll change it for you this time.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

This is exactly what a constructor is for - to validate the data you store in your object.

You will need to decide what is the appropriate thing to do if someone tries to make an object with a value of -4. Will your program crash? set a default value of 0? return a null object (which may have other consequences...)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

It's possible, but your own data type will not work like one of the built-in primitive types - Java has a fixed set of 8 primitive types (byte, short, int, long, char, boolean, float, double), you can't add your own primitive types. Also, Java has no operator overloading so you will not be able to define your own +, -, *, / and other operators for your own unsigned integer data type.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
As per what i have tried, we can check for positive integers using constructor. But, if program is using primitive int data type, i could not think any way we can restrict it to positive integers.

Please suggest if their is any other way for restricting to positive int/integers.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Raj Kumar Bindal wrote:As per what i have tried...

Which is...?

There are ways to do this, but we need to know where exactly you are running into problems.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

they can always pass in a negative number, but you don't have to accept/save it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Positive integer data type in java