Skip to content

JAVA – string methods – 1

Java is rich with methods to handle strings and strings manipulation. In this post I will detail the most used string methods and usage of each one. As string functions are much and important I will make this post divided to several posts in a series to be easy to follow and to give time to exercise. 

length() 

This method Returns the length of the sequence of characters represented by this object.

Syntax:

public int length();

Continue reading “JAVA – string methods – 1” »

JAVA BEGINNERS TUTORIAL – Working with Strings

Strings are a sequence of characters. In the Java, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Creating Strings:

The most direct way to create a string is to write:

String greeting = "Hello Java!";

Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello Java!'.

As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.

Continue reading “JAVA BEGINNERS TUTORIAL – Working with Strings” »

JAVA BEGINNERS TUTORIAL – Working with Characters

when we need to use one character and dealwith it we use primitive data types char.

char ch = 'a';

// Unicode for the arrow ↘ as character
char uniChar = '\u2198'; 

// an array of chars
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' }; 

in development, we come across some situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char.

The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor:

Character ch = new Character('a');

The Java compiler will also create a Character object for you under some circumstances. For example, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you. This feature is called autoboxing—or unboxing, if the conversion goes the other way.

Continue reading “JAVA BEGINNERS TUTORIAL – Working with Characters” »

JAVA BEGINNERS TUTORIAL – Dealing With Numbers 3

this how I review basics of a language to write what I reviewed in blog posts also it will be good interance for Android development. the good news now it become dialy as I need to review also other languages before release some open source case studies this blog post is the last one of numbers series lets view some methods.

min()

The method gives the smaller of the two arguments. The argument can be int, float, long, double.

double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)
  • A primitive data types
  • This method Returns the smaller of the two arguments.

Example:

public class Test{ 

   public static void main(String args[]){
      System.out.println(Math.min(12.123, 12.456));      
      System.out.println(Math.min(23.12, 23.0));  
   }
}

max()

The method gives the maximum of the two arguments. The argument can be int, float, long, double.

double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)
  • A primitive data types
  • This method Returns the maximum of the two arguments.

Example:

public class Test{ 

   public static void main(String args[]){
      System.out.println(Math.max(12.123, 12.456));      
      System.out.println(Math.max(23.12, 23.0));  
   }
}

Continue reading “JAVA BEGINNERS TUTORIAL – Dealing With Numbers 3” »

JAVA BEGINNERS TUTORIAL – Dealing With Numbers 2

Back to number methods and more function to discuss :) 

toString()

The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is return. If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned.

String toString()
static String toString(int i)
  • toString(): This returns a String object representing the value of this Integer.
  • toString(int i): This returns a String object representing the specified integer i.

Example:

public class Test{ 

   public static void main(String args[]){
      Integer x = 5;
      System.out.println(x.toString());  
      System.out.println(Integer.toString(12)); 
   }
}

Continue reading “JAVA BEGINNERS TUTORIAL – Dealing With Numbers 2” »

JAVA BEGINNERS TUTORIAL – Dealing With Numbers 1

From time to time I need to have a peek on the basics of any language I know for that reason I am wrting these toturial blogs. any programming language must have to Work with numbers. in java is easy as we use primitive data types such as byte, int, long, double, etc. 

Example:

int i = 5000;
float gpa = 13.65;
byte mask = 0xaf;

In development, There are reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes "wrap" the primitive in an object. All of the numeric wrapper classes (Integer, Long, Byte, Double, Float, Short)  are subclasses of the abstract class Number:

OOP WrapperClass JAVA BEGINNERS TUTORIAL   Dealing With Numbers 1

Continue reading “JAVA BEGINNERS TUTORIAL – Dealing With Numbers 1” »

JAVA BEGINNERS TUTORIAL – Branching Statements

The break Statement:

The break Statement is used to stop the entire loop (for loop, while loop or do-while) or a switch statement. The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax:

break;

Example:

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
	      break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

The continue Statement:

The continue Statement can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

  • In a for loop, the continue keyword causes flow of control to immediately jump to the update statement.
  • In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression.

Syntax:

continue;

Example:

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
	      continue;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

JAVA BEGINNERS TUTORIAL – Loops

sometimes we need to execute a block of code in several recurrent times. this action called a loop and for this action Java have some Loop statments as the following:

while Loop:

A while loop is a control structure that allows you to repeat a task a certain number of times. and it means while an expression is true do the action in the block code and do it again and again till the expression result is false.

Syntax:

while(expression)
{
   //Statements
}

Example:

public class Test {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

Continue reading “JAVA BEGINNERS TUTORIAL – Loops” »

JAVA BEGINNERS TUTORIAL – Conditions

Programming is conditions and it is the main core of start writing a code as when you think in any problem you will find that you need to take decision for that we have decision making statments as the following:

The if-then Statement:

An if statement consists of an expression followed by one or more statements. this expression evaluates a boolean result (TRUE or FALSE).

Syntax:

The syntax of an if statement is:

if(expression)
{
   //Statements will execute if the Boolean expression is true
}

If the expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Example:

public class Test {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("This is if statement");
      }
   }
}

This would produce the following result:

This is if statement

The if-then-else Statement:

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax:

The syntax of an if…else is:

if(expression){
   //Executes when the Boolean expression is true
}else{
   //Executes when the Boolean expression is false
}

Example:

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("This is if statement");
      }else{
         System.out.print("This is else statement");
      }
   }
}

The if-then-else if-then-else Statement:

An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.

When using if , else if , else statements there are few points to keep in mind.

  • An if can have zero or one else's and it must come after any else if's.

  • An if can have zero to many else if's and they must come before the else.

  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax:

The syntax of an if…else is:

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
}else {
   //Executes when the none of the above condition is true.
}

Example:

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("This is else statement");
      }
   }
}

The switch Statement:

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax:

The syntax of enhanced for loop is:


switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}

The following rules apply to a switch statement:

  • The variable used in a switch statement can only be a byte, short, int, or char.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall throughto subsequent cases until a break is reached.

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example:

public class Test {

   public static void main(String args[]){
      //char grade = args[0].charAt(0);
      char grade = 'C';

      switch(grade)
      {
         case 'A' :
            System.out.println("Excellent!"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}