String Arrays in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle String arraysExample-2: To print fist character of every names String arrays Steps: Step-1: Declare String object and call the methode getNames() Step-2: Use for loop to print Step-3: Inside for loop, check if the string is null Step-4: If string not null, then pring names usring Sysem.out.println(names[i]) Here after main() will be the method definition for getNames() Example-1: package com.example; public class arrays { public static void main(String[] args) { String [] names = getNames(); for(int i=0; i<names.length;i++) { if (names[i] != null) { System.out.println(names[i]; } } } /** * method to input String array * Unlike an array where by default all element get initialized to ‘0’ * In an String array by default all the elements are initialized to null * @return names */ public static String[] getNames() { String[] names = new String[5]; names[0] = “Abhay”; names[1] = “Banwari Lal”; names[2] = “Chanchal”; names[3] = “devender”; names[4] = “Ekta” //the last element of an array will cntain a special value null values; remember in numeric arrays, when we created array they by default cntained 0’s return names; } } Example-2: To print fist character of every names package com.example; public class arrays { public static void main(String[] args) { String [] names = getNames(); for(int i=0; i<names.length;i++) { if (names[i] != null) { System.out.println(names[i].charAt(0); } } } /** * method to input String array * Unlike an array where by default all element get initialized to ‘0’ * In an String array by default all the elements are initialized to null * @return names */ public static String[] getNames() { String[] names = new String[5]; names[0] = “Abhay”; names[1] = “Banwari Lal”; names[2] = “Chanchal”; names[3] = “devender”; //the last element of an array will cntain a special value null values; remember in numeric arrays, when we created array they by default cntained 0’s return names; } } Share on Facebook Share Send email Mail Print Print