Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,972 questions

51,915 answers

573 users

How to get string length in Java

4 Answers

0 votes
class Program {
    public static void main(String[] args) {
        try {
 
            String s1 = "Java";
            String s2 = "Programming";
            String s3 = s1 + " " + s2;
 
            int l1 = s1.length();
            int l2 = s2.length();
            int l3 = s3.length();
 
            System.out.println(l1);
            System.out.println(l2);
            System.out.println(l3);
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}


 
/*
              
run:
        
4
11
16
     
*/

 



answered Nov 24, 2016 by avibootz
edited Apr 7, 2024 by avibootz
0 votes
class Program {
    public static void main(String[] args) {
        try {
 
            int len = "java".length();
 
            System.out.println(len);
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}


 
/*
              
run:
        
4

*/

 



answered Nov 24, 2016 by avibootz
edited Apr 7, 2024 by avibootz
0 votes
class Program {
    public static void main(String[] args) {
        try {
 
            String s = "";
 
            int len = s.length();
            System.out.println(len);
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}


 
/*
              
run:
        
0

*/

 



answered Nov 24, 2016 by avibootz
edited Apr 7, 2024 by avibootz
0 votes
class Program {
    public static void main(String[] args) {
        try {
 
            String s = null;
 
            int l = s.length();
            System.out.println(l);
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}


 
/*
              
run:
        
java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null

*/

 



answered Nov 24, 2016 by avibootz
edited Apr 7, 2024 by avibootz

Related questions

2 answers 185 views
1 answer 161 views
161 views asked Oct 24, 2016 by avibootz
1 answer 117 views
117 views asked Jan 15, 2016 by avibootz
1 answer 123 views
123 views asked Jan 12, 2016 by avibootz
1 answer 129 views
1 answer 114 views
114 views asked Sep 22, 2022 by avibootz
1 answer 145 views
...