New course! Every coder should learn Generative AI!
Try a free lesson+ 14
A variable can be an object which can hold two or more fields/values.
+ 11
You can use array to asign many values to only one variable
+ 10
I explored this question a while back for C++. If your question is similar to mine, I'd say the answer is no since Java has no abilities to work with pointers.
https://www.sololearn.com/Discuss/1185580/?ref=app
+ 9
Normally not, except as an object like intended by Sonic .
But with workarounds yes, it doesn't make too much sense today but in the past, when system resources was limited it did.
Example:
Store a bool, a 16 colours value and a char inside a 2Bytes int
128 64 32 16 8 4 2 1 8 4 2 1 b
F. E. D. C. B A 9 8 7 6 5 4 3 2 1 0
Then you access the bool as var%2
The 16 int as (var/2)%16
And the char as (var/32)%256 or 128
Or use bit shift operator >>
As a further example here is an Atari code-golf challenge where a small shape is stored inside a bitmap matrix.
The code is hard to read due to golf requirements but the link to the challenge explain it well.
https://code.sololearn.com/c0V9IjKK56P8/?ref=app
+ 6
You can also allocate memory for two values and use pointers but it still doesn't make much sense.
Personally I think the real and useful concept here is not "store" but "refer" one or more value to an entity and that's what structs and classes too can be used for.
struct Person{
string name;
int age;
...
};
=> p.name p.age ....
+ 5
No, but you could in a way store multiple values as a string and then split the string and store each element inside an array I do this alot in my codes.
simplest way is just to create a class then you can use the refrenece stored inside a variable to accsess hundreds of variables in that object 😊
+ 4
Since people are posting without checking if a similar answer has already been posted and the original person hasn't followed up to clarify their question, I'm unfollowing this thread.
If anyone needs my attention, please be sure to mention me... assuming you're someone who checks previous posts. 😉
+ 3
in C/C++ got unions to store one value in two vars,
but two values in a single var, maybe combine unions + Ptrs + arrays.
in java idk.
another more raw method is by using a floating point unit.
the units are one numeric value and the decimals another.
seems to be only for integers, characters and bools.
but not with string and other floatings.
additionally you need a func or class that "unpack" them.
+ 3
Gangadhar What is the purpose of your question? Maybe we're not even answering the question you meant to ask.
Can you provide more details or an example of what you're trying to accomplish? What prompted you to ask this question?
+ 2
yes you can! for intance, you could use a float to save an integer and a value from 0 to 1 in its decimals. for intance, an index numbering a voxel in an image and its correlation to data in other images. whether this is useful or not will depend on your particular algorithm, you may not need to unpack even.
+ 2
Nope, it's possible to store only one value for a single variable, anyway if you want to store two ore more values you could use an array(which could store more values of the same type)
int [] array = { 1,2,3,4}
System.out.println(array[0])
//The output is the number 1
+ 2
Hmm.. I think array can hold collection of similar values in single variable....
Please confirm??
+ 2
In an array you can store objects of more than one dimension.
In a string separated by apostrophes, you can split it to return an array of n strings where n is a positive integer 1, 2, 3 etc. 🤔
+ 1
those things ever made me dream w PCs that can do all by brute force, without limitations, but open my eyes and I crash w my dual core.
maybe in the future, but we will still alive?
+ 1
use array, or u cannot put multiple value in one variable
+ 1
No, because as in mathematics, a function to be 1to1 must has unique variables. Although, you can use an arrey, or a list, depending on the programing language you use.
0
It depends on values sizes. In some cases it possible when you use bitwise operations. Once I stored week states in one byte variable.
0
Technically, you could encode both values into a string and store that string in a single variable. However, this would be a generally bad idea because you’ll have to constantly encode and decode the values and it would be so much simpler just to use two variables or an array.
0
You can use Array.
So you can even store unlimited data it in.
- 1
please visit fiek.cf my new website