InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Java tutorials & examples

    Java Basics

    • Hello World
    • print and println
    • System.out.format
    • static and instance variables

    Java classes and objects

    • this keyword

    Java Strings

    • Initialize string array
    • String prefix match
    • compare string
    • string to int

    Java Arrays

    • Array basics
    • 2d array example
    • array for-each loop
    • fill an array

    Java cookbooks

    • method equals vs operator ==
    • print exception as string
    • Arrays.sort custom Comparator
    • create inline Comparator
     
    • Home
    • > Tutorials
    • > Java

    Java – array basics

    on Jun 18, 2016

    Initialize arrat and print length

    public class Main {
      public static void main(String[] args) {
        int[] arr = {10, 21, 3};
        System.out.println(arr.length);
      }
    }
    3
    

    Access element at specific index

    public class Main2 {
      public static void main(String[] args) {
        int[] arr = {10, 21, 3};
        // Print element at index 1
        // index starts from 0
        System.out.println(arr[1]);
      }
    }
    21
    

    Access non existing index

    This will throw Index Out Of Bounds Exception

    public class Main3 {
      public static void main(String[] args) {
        int[] arr = {10, 21, 3};
        // index out of bound
        System.out.println(arr[10]);
      }
    }
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    	at Main3.main(Main3.java:5)
    

    Suggested posts:

    1. Python re (regex) search examples
    2. Java – fill an array using java.util.Arrays
    3. Mac – print java home on command line
    4. Java – initialize string array
    5. Java – compare strings
    6. Mac – show volume icon on menu bar
    7. CSS padding inside a box
    8. Java System.out.print and println
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Java
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress