What type of variable that can contain 1,000,000,000(a decimal number) takes the most memory space?
- int in C
- string in C
- string in Java(which uses unicode)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A Java String. Under the hood A Java String consists of an object with 3 fields, one of which points to a separate array object containing the characters. Plus of course, Java Strings are composed of 16 bit characters1.
If you are worried about memory usage over all other criteria, don’t use Java. But for most applications, memory usage is the least of your concerns.
It is worth noting that 1,000,000,000 can be represented using a Java
intwhich will be the same size as a C signed or unsigned (32 bit) integer.Furthermore, a C
intis not necessarily big enough to represent 1,000,000,000. On some platforms,intis 16 bits, and this is allowed by the C standard.1 – Actually, this is Java platform dependent. For example, in Java 9 they modified the
Stringimplementation to use onebyteper character for strings that are composed entirely of characters in the range 0 to 255. See this article. But despite this, a Java string still takes more space than a C string.