+ 7

Is it possible to store two values in a single variable?

11th Aug 2019, 5:40 AM
Puram Gangadhar
Puram Gangadhar - avatar
20 Answers
+ 11
You can use array to asign many values to only one variable
11th Aug 2019, 3:53 PM
Aung Thiha
Aung Thiha - avatar
+ 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
11th Aug 2019, 7:14 AM
David Carroll
David Carroll - avatar
+ 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
11th Aug 2019, 8:47 AM
AZTECCO
AZTECCO - avatar
+ 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 ....
11th Aug 2019, 9:42 AM
AZTECCO
AZTECCO - avatar
+ 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 ๐Ÿ˜Š
11th Aug 2019, 7:05 AM
D_Stark
D_Stark - avatar
+ 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. ๐Ÿ˜‰
11th Aug 2019, 6:21 PM
David Carroll
David Carroll - avatar
+ 4
If its an object or an array, yea you can.
12th Aug 2019, 11:24 PM
Petros the Parakeet
Petros the Parakeet - avatar
+ 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.
11th Aug 2019, 8:46 AM
BinaryEden
BinaryEden - avatar
+ 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?
11th Aug 2019, 3:46 PM
David Carroll
David Carroll - avatar
+ 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.
11th Aug 2019, 9:52 AM
bell
bell - avatar
+ 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
11th Aug 2019, 4:55 PM
Antonio Battista
Antonio Battista - avatar
+ 2
Hmm.. I think array can hold collection of similar values in single variable.... Please confirm??
11th Aug 2019, 11:43 PM
Karthik Marimuthu MCA
Karthik Marimuthu MCA - avatar
+ 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. ๐Ÿค”
12th Aug 2019, 4:29 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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?
11th Aug 2019, 12:22 PM
BinaryEden
BinaryEden - avatar
+ 1
use array, or u cannot put multiple value in one variable
12th Aug 2019, 3:46 PM
PVSV PW
PVSV PW - avatar
+ 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.
12th Aug 2019, 8:35 PM
Konstantinos Galogavras
Konstantinos Galogavras - avatar
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.
11th Aug 2019, 9:53 PM
Alex Ziko
Alex Ziko - avatar
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.
11th Aug 2019, 10:38 PM
Jason Stone
Jason Stone - avatar
0
You can use Array. So you can even store unlimited data it in.
23rd Aug 2019, 8:10 PM
Ali
- 1
please visit fiek.cf my new website
14th Aug 2019, 1:15 PM
Bibidh