• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Positive integer data type in java

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure that is possible.

What have you tried so far?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they can always pass in a negative number, but you don't have to accept/save it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic