Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Positive integer data type in java

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure that is possible.

What have you tried so far?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they can always pass in a negative number, but you don't have to accept/save it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic