Ascii value of a to z Java program

 Understanding ASCII and How to Print ASCII Values in Java

ASCII is a character encoding standard: It is used to represent text in computers and other devices. Each of its characters is assigned a unique numerical value comprising, amongst others, letters, digits, punctuation and control. For example, the ASCII value of the letter 'A' is 65, and of the letter 'a' is 97.

In this blog post I will explain a simple Java program that outputs the ASCII values for lower case letters deathnbsp;'a' to 'z'; this helps you understand how characters are actually represented by the computer as well as work with ASCII values in Java.

What is ASCII

ASCII is a 7-bit character encoding standard that can represent 128 characters (0 to 127). These characters include:

Uppercase letters: A to Z (ASCII values 65 to 90)

Lowercase letters: a to z (ASCII values 97 to 122)

Digits: 0 to 9 (ASCII values 48 to 57)

Special characters: !, @, #, $, etc.

Control characters: Newline (\n), tab (\t), etc.

Subsequently, when writing programs for programming matrix (i.e. application software), you will want to use ASCII values instead of characters themselves in order to handle different character sets: for example to sort escort compare different characters.

Java program to print ASCII values from a to z

public class AsciiValues {

    public static void main(String[] args) {

        // Loop through characters 'a' to 'z'

        for (char ch = 'a'; ch <= 'z'; ch++) {

            // Print the character and its ASCII value

            System.out.println("Character: " + ch + ", ASCII Value: " + (int) ch);

        }

    }

}

semicolon in java

 a semicolon (;) is used to separate statements in a program. Each statement in a Java program must end with a semicolon, just like in many other programming languages.

Here's an example of  semicolon in a Java program:

int x = 5; // Declare a variable and assign a value

System.out.println(x); // Print the value of the variable

x++; // Increment the value of the variable

System.out.println(x); // Print the new value of the variable

In this example, there are three statements: one for declaring a variable and assigning a value, one for printing the value of the variable, and one for incrementing the value of the variable. Each statement ends with a semicolon, which separates them from each other.

It's worth noting that, in Java, a semicolon is not always necessary. For example, if you are defining a class or method, you don't need to use a semicolon at the end of the definition. Also, you don't need to use a semicolon after a block statement (statements enclosed in curly braces {}) that is part of a control statement such as if-else, while, for, etc. The end of the block statement is represented by the closing curly brace.

In the case of a single-line if statement or a single-line loop, you can also omit the curly braces {} and write the statement directly after the if or while keyword, respectively.

if(x==5) System.out.println("x is 5");

while(x<10) x++;

In summary, a semicolon is used in Java to separate statements and mark the end of a statement. However, it's not always required and depends on the context of the statement or block.

It's worth noting that in Java, a semicolon is also used in certain control statements such as the "for" loop and the "enhanced for" (or "for-each") loop.

In a "for" loop, the initialization, condition, and increment/decrement statements are separated by semicolons. 


In this example, the initialization statement "int i = 0" is executed once before the loop starts, the condition "i < 10" is checked before each iteration, and the increment statement "i++" is executed after each iteration. Each of these statements is separated by a semicolon.

In the case of the "enhanced for" loop, the semicolon is used to separate the loop variable and the array or collection that is being iterated over.

Instance variables in java with example program

  • Variables declared inside a class and outside method without static keyword known as instance variables.
  • Instance variables will be used by objects to store state of the object.
  • Every object will have their own copy of instance variables. where as static variables will be single and shared(accessed) among objects.
  • Instance variables will be associated with the instance(Object)
  • Static variables will be associated with the class.



#1: Java example program on declaring and accessing instance variables.

  1. package com.instanceofjava.instancevariables;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview questions
  5.  * 
  6.  * Description: Instance variables in java with example program
  7.  *
  8.  */
  9. public class InstanceVariables {

  10. String websiteName;
  11. String category;
  12. public static void main(String[] args) {
  13. InstanceVariables obj = new InstanceVariables();
  14. obj.websiteName="www.InstanceOfJava.com";
  15. obj.category="Java tutorial/interview questions";

  16. }

  17. }


  • Instance variables allows all four type of access specifiers in java


instance variables in java with example



  • Instance variables can be final and transient


instance variables in java with example program



  • Instance variables can not be declared as abstract, static strictfp, synchronized and native

Java read file line by line example program

  • Reading a text file line by line in java can be done by using java.io.BufferedReader .
  • Create a BufferedReader class by passing new FileReader(new File("filename")) object to it's constructor.
  • By using readLine() method of  BufferedReader class we can read line by line text from a text file as Strings.
  • Lets see a java example program on hoe to read data from file line by line using BufferedReader class readLine() method.
  • Java read lines from text file example program.


#1: Java Example program to read file line by line

 

  1. package com.instanceofjava.readfile;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. /** *  * 
  8. @author www.instanceofjava.com 
  9. * @category: Interview Programs 
  10. * @description: How to read text file line by line in java example program 
  11. * */
  12.  
  13. public class ReadFile {
  14.  
  15.     public static void main(String[] args) {        
  16.         
  17.         try {            
  18.            File fileName = new File("E:\\data.txt");
  19.             FileReader fileReader = new FileReader(fileName);     
  20.             BufferedReader bufferedreader = new BufferedReader(fileReader);           
  21.             StringBuffer sb = new StringBuffer();
  22.             String strLine;            
  23.            while ((strLine = bufferedreader.readLine()) != null) {
  24.                 sb.append(strLine);
  25.                 sb.append("\n");        
  26.             }         
  27.            fileReader.close();
  28.            System.out.println(sb.toString()); 
  29.          } catch (IOException e) {
  30.             e.printStackTrace();     
  31.    }
  32.  
  33.     }
  34.  
  35. }

Output:

  1. java read file line by line example
  2. java read file line by line java 8
  3. java read lines from text file 
  4. example bufferedreader java example

#2: Java example program to read text file line by line using java 7 try with resource example



read file line by line java


#3: Java example program to read text file line by line using java 8 example (Using stream)

  1. package com.instanceofjava.readfile;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.stream.Stream;
  6. /**
  7.  *
  8. * @author www.instanceofjava.com
  9.  * @category: Interview Programs 
  10. * @description: How to read text file line by line in java example program using java 8 stream
  11. */
  12. public class ReadFile {
  13. public static void main(String[] args) throws IOException {
  14.  
  15. try (Stream<String> stream = Files.lines(Paths.get("E:\\data.txt"))) 
  16. {           
  17.  
  18.  stream.forEach(System.out::println);
  19.  
  20. }
  21. }
  22. }

Convert to maven project in eclipse java

  • In order to convert any java project in to maven using eclipse we need to install m2e plugin.
  • Latest versions of eclipse are coming with this m2e plugin by default.
  • If not found this plugin we can install it by help->install->click on add and enter http://download.eclipse.org/technology/m2e/releases. 
  • currently i am using eclipse oxygen version so it is having m2e plugin.
  • I have created a normal java project and i want to convert that into a maven project.
  • To convert any project in to maven right click on the project and ->configure->convert to maven project. 
  • After that a window will be opened and you need to enter group id and artifact id and then click on finish.  
  • convert to maven project eclipse luna / convert to maven project is not visible in eclipse /convert to maven project option not available in eclipse.



Right click on project and select configure-> convert to maven project.


convert to maven project java

Project will be converted to maven by creating pom.xml file.

how to convert to maven project java

 

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or
  2. /2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  3. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.   <modelVersion>4.0.0</modelVersion>
  5.   <groupId>com.instanceofjava.maven</groupId>
  6.   <artifactId>MavenProject</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <build>
  9.     <sourceDirectory>src</sourceDirectory>
  10.     <plugins>
  11.       <plugin>
  12.         <artifactId>maven-compiler-plugin</artifactId>
  13.         <version>3.7.0</version>
  14.         <configuration>
  15.           <source>1.8</source>
  16.           <target>1.8</target>
  17.         </configuration>
  18.       </plugin>
  19.     </plugins>
  20.   </build>
  21. </project>

Add dependencies to your pom.xml file

  1.  <properties>
  2.         <springVersion>5.0.3.RELEASE</springVersion>
  3.       </properties>
  4.       <dependencies>
  5.         <dependency>
  6.           <groupId>org.springframework</groupId>
  7.           <artifactId>spring-context</artifactId>
  8.           <version>${springVersion}</version>
  9.         </dependency>
  10.        <dependency>
  11.          <groupId>org.springframework</groupId>
  12.          <artifactId>spring-core</artifactId>
  13.           <version>5.0.3.RELEASE</version>
  14.         </dependency>
  15.       </dependencies>

Java finally block after return statement

  • finally block will be executed always even exception occurs.
  • If we explicitly call System.exit() method then only finally block will not be executed if exit() call is before finally block.
  • There are few more rare scenarios where finally block will not be executed.
  1. When JVM crashes
  2. When there is an infinite loop
  3. When we kill the process ex:kill -9 (on unix)
  4. Power failure, hardware error or system crash.
  5. And as we discussed System.exit().
  •  Other than these conditions finally block will be executed always.
  • finally block is meant to keep cleaning of resources. Placing code other than cleaning is a bad practice
  • What happens if we place finally block after return statement? or
  • Does finally block will execute after return statement??
  •  As per the above rules Yes finally will always execute except any interruption like System.exit()  or all above mentioned conditions

 Program #1 : Java example program which explains how finally block will be executed even after a return statement in a method.

  1. package com.instanceofjava.finallyblockreturn;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: does finally gets executed after return statement??
  5.  *
  6.  */
  7. public class FinallyBlockAfterReturn {
  8.  
  9.     public static int Calc(){
  10.         try {
  11.             
  12.             return 0;
  13.         } catch (Exception e) {
  14.             return 1;
  15.         }
  16.  
  17. finally{
  18.             
  19.  System.out.println("finally block will be executed even when we
  20. place after return statement");
  21.         }
  22.  }
  23.     
  24. public static void main(String[] args) {
  25.         
  26.         System.out.println(Calc());         
  27. }
  28.  
  29. }



  
Output:


  1. finally block will be executed even when we place after return statement
  2. 0


 Can we place return statement in finally??
  • Yes we can place return statement in finally and finally block return statement will be executed.
  • But it is a very bad practice to place return statement in finally block.
Program #2 : Java example program which explains finally block with return statement in a method.



finally block after return statement

Advantages and disadvantages of arrays in java


Disadvantages of array in java
  • Arrays are Strongly Typed.
  • Arrays does not have add or remove methods.
  • We need to mention the size of the array. Fixed length.
  • So there is a chance of memory wastage.
  • To delete an element in an array we need to traverse through out the array so this will reduce performance.
  • Arrays have a fixed size, which means that their size cannot be changed once they are created. This can be a disadvantage when working with dynamic data sets that need to grow or shrink in size.
  • Arrays are not suitable for storing large numbers of different types of elements, it is best used when the data is of a single type.
  • Insertion and deletion operations on arrays can be slow, as they require shifting elements to make room for new elements or close the gap left by deleted elements.
  • Accessing an element in an array can be slow if the array is large, and the index of the element is not known in advance.



Advantages of arrays:


  • We can access any element randomly by using indexes provided by arrays.
  • Primitive type to wrapper classes object conversion will not happen so it is fast.
  • Array can store many number of elements at a time.
  • Arrays have a fixed size, which means that their size cannot be changed once they are created. This can make it easier to manage memory usage and avoid runtime errors caused by dynamic memory allocation.
  • Arrays are relatively fast and efficient. They have a constant time complexity for basic operations such as accessing and updating elements, which makes them suitable for large data sets.
  • Arrays are easy to understand and use, and they are supported by many built-in methods in the Java API, such as Arrays.sort(), Arrays.binarySearch(), and Arrays.toString().



Arrays in java   InstanceOfJava

Java interview programs on arrays:

  • Check below for some of the interesting java interview programs on arrays.

Five different ways to print arrays in java:



Creating array of objects in java:



Java program to find missing numbers in an array:



Find second highest number in java arrays:




Convert arrayList to array:

Copy all elements of  hash set to Object array:


Select Menu