×

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The string class consists of characters. String can’t be changed if it is created. The objects of the string can’t be changed because they are immutable.

Example :

String s=“Increase”;

String s can be converted into character array as

char ch[]={‘I’,’n’,’c’,’r’,’e’,’ a’,’s’,’e’};

String s =new String(ch);

In the above example, the String class has the length() method, which is used to find the length of the string. For finding the length of the string, there is the length() method.

The length () method returns the length of the string, which is an integer data type. This method counts the number of characters in the string.

Java uses UTF-16 for the representation of the character. Each character occupies two bytes.

The size of int is 4 bytes. The range of integers is -2^31 to 2^31-1. This means (-2147483648 to 2147483647) accepts this range; we can’t be able to store the 2147483647th character. Hence maximum length should be from 0 to 2147483647.

We can find the maximum length using the java program.

Example

import java.util.Arrays;  
public class MaximumSize   
{  
public static void main(String args[])   
{  
for (int i = 0; i < 1000; i++)   
{  
try   
{  
//Integer.max value stores maximum value of the string 
char[] array = new char[Integer.MAX_VALUE - i];  
//specific value is arranged to each datatype  
Arrays.fill(array, 'a');  
//Constructor is created for String class ang passing array as an argument
String str = new String(array);  
//prints the string length  
System.out.println(str.length());  
}   
catch (Throwable e)   
{  
// throws a message  
System.out.println(e.getMessage());  
//returns the maximum value of string
System.out.println("Max: " + (Integer.MAX_VALUE - i));  
System.out.println("Max: " + i);  
}  
}  
}  
} 

Output

Maximum Length Of String In Java

As the output, for example, users can run 1000 times. In the exception occurring block

An array of integers.MAX_VAlue-i. After that, we used the fill method to use the Array class.

The fill method assigns a particular data type value for each array element.

In the example, the program catch block is used to throw an exception caught in the try block. The getMessage() method in the catch block throws an exception.

Each character in Java stores two bytes of memory because strings can be stored as UTF-16 codes. We need twice the memory for storing old and new strings to add strings.

If we insert the strings more than their size, the memory will overflow, and we get a negative result. Consider an example for extending the string beyond the limit.

//Program for beyond the size

public class SizeBeyondLimit  
{   
public static void main(String[] arg)   
{   
try   
{   
System.out.println( "Trying to initialize" + " a n with value" + " Integer.MAX_VALUE + 1");   
// to store maximum integer value  
int n = Integer.MAX_VALUE + 1;   
 // preturn the result as negative num as size exceeded 
System.out.println("n = " + n);   
}   
catch(Exception e)   //used for handling the exception
{   
System.out.println(e);   
}   
}   
}  

Output

Maximum Length Of String In Java

From the output of the above program, we can see that it was printed as a negative integer.

Because of exceeding the actual size of the String.


Related Topics

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Method chaining in Java

Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference...

3 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.