How many bytes in memory a string occupy in Java language?
Suppose i write:
String name="Bill Gates";
So, how much memory it requires? Whether it can store a UNICODE character in the string. How internally it is implemented, using a 1 byte char array or 2 byte unicode char array? I read somewhere that Java's char data type is 16 bit wide.
Thanks!!!
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

How many bytes in memory a string occupy in Java language?
Started by Alejandro, Nov 28 2014 03:28 AM
online printing
6 replies to this topic
#1
Posted 28 November 2014 - 03:28 AM
#2
Posted 28 November 2014 - 10:17 AM
It's 2 bytes per char. Both UTF8 and Unicode occupies 2 bytes just the same, even when UTF could be using just 1 . Could be because the second byte could be used for the code page, because code page determines which UTF8 char-set are you using, which varies on location/language.
#3
Posted 28 November 2014 - 01:44 PM
Expanding upon what BlackRabbit said, if you don't want/can't have 2 bytes per character, you could always store the data in an array of bytes, and have it so that 1 byte represents 1 character. However, if you do this you'll be limited to only 256 different character possibilities. For example:
//to convert a character array to a byte array of the same length: public static byte[] characterArrayToByteArray(char[] charArray) { byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) { byteArray[i] = (byte) (charArray[i]); } return byteArray; } //and to convert it back into character array. public static char[] byteArrayToCharacterArray(byte[] byteArray) { char[] charArray = new char[byteArray.length]; for (int i = 0; i < byteArray.length; i++) { charArray[i] = (char) (Byte.toUnsignedInt(byteArray[i])); } return charArray; }
Both examples are using Jdk1.8.0_25. Anything before a certain Java 8 version wont have the method "toUnsignedInt" in Byte. If you need to ensure that a byte is unsigned prior to 1.8, you can use the simple declaration (int) (myByte & 0xFF)
Speaks fluent Java
#4
Posted 28 November 2014 - 01:56 PM
If you want to use byte[] rather than char[] - you should encode/decode to the correct format. Doing byte b = (byte)c; isn't great.
Unless you know that you only every build ascii strings and want to convert between them. But then you might aswell just use a byte array.
I don't believe anyone really heavily uses char[]'s in java anyway.
Creating SEGFAULTs since 1995.
#5
Posted 01 December 2014 - 10:40 PM
I think String are unicode, so 16bits
#6
Posted 04 December 2014 - 02:41 PM
You also should be aware that String is internally optimized in Java, so that when you create many instances of "Bill Gates" they might share the memory space / instance, but it is not certainly true in every case. This is might be true for other immutable objects as well.
#7
Posted 04 December 2014 - 03:56 PM
Yes because it is immutable it could be flyweight
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download