SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES

FORGOT YOUR PASSWORD?

FORGOT YOUR DETAILS?

AAH, WAIT, I REMEMBER NOW!
Openbaraza

Openbaraza

Openbaraza

Baraza is a Swahili word that means a deliberation meeting held by a collective group of people with wisdom.

T : +254 (20) 2243097 / 2227100
Email: info@openbaraza.org

OPENBARAZA
Dew CIS Solutions LTD Barclays Plaza, 12th Floor. Loita Street.

Open in Google Maps
  • Framework
  • Projects
    • Human Resource
      • Human Resource
      • Time Attendance
      • Project Planning
    • Finance Management
      • Business
      • Payroll
    • Investment Management
      • Banking
      • Chama
      • Sacco
    • Case Management
      • Judiciary
    • Real Estate Management
      • Property
    • Academic Management
      • UMIS
      • Schools
  • Forums
    • HCM HR Payroll
    • Business
    • Sacco
    • Banking
  • Blog & News
    • OpenBaraza Framework
    • Databases
    • Java Programming
    • Scrum
  • Contact Us
  • Home
  • Blog & News
  • Java Programming
  • 05. Variables
March 31, 2023

05. Variables

05. Variables

by dennis@dennis.me.ke / Friday, 19 July 2019 / Published in Java Programming

 

Having covered a basic introduction in Java Programming language and structure of a typical Java Application in the previous blogs, this blog takes a deeper look into Java variables, their types and use.

What is a variable?

A variable is a named memory location that holds a value while a program is executed.

Types of Variables

Java has three types of variables based on the scope and the location within the program. These are: local, instance and static variables.

Local Variables. Are variables declared inside the body of a method? Local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. There is no special keyword designating a variable as local and the static keyword cannot be used on them.

Instance Variables (Non-Static Fields). This is a field declared inside the class but outside the body of the method. It is not declared as static. A non-static variable contains values that are unique to each instance (object) of a class and is not shared among instances.

Class Variables (Static Fields). A class variable is any field declared with the static modifier. This means that only a single copy of static variable is created and is shared among all the instances (objects) of the class.

For more information about variables in java visit https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

LIVE A LITTLE

Why did the WiFi and the computer get married?

Because they had a connection.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Using a quadratic equation, we shall try to calculate the roots of a quadratic equation to give a clear picture of how variables are used in java.

Write the following code:


package quadratic

import java.util.Scanner;

public class Quadratic {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Input a: ");

double a = input.nextDouble();

System.out.print("Input b: ");

double b = input.nextDouble();

System.out.print("Input c: ");

double c = input.nextDouble();

double root1 = ((-b) + Math.sqrt(b * b - (4 * a * c))) / 2 * a;

System.out.println("The value of root1 is: " + root1);

double root2 = ((-b) - Math.sqrt(b * b - (4 * a * c))) / 2 * a;

System.out.println("The value of root2 is: " + root2);

}

}

Understanding the code:


//This invokes a package “Scanner” that allows user input when prompted

import java.util.Scanner;

/*Create an object of a “Scanner” class which is define in import “java.util.Scanner” package. Scanner class allows user to take input from console.*/

Scanner input = new Scanner(System.in);

//Prompt user for terms a, b and c respectively and store them in variables of data type “double”:

System.out.print("Input a: ");

double a = input.nextDouble();

System.out.print("Input b: ");

double b = input.nextDouble();

System.out.print("Input c: ");

double c = input.nextDouble();

 

//Quadratic formula that derives root1

//“Math.sqrt” invokes a square root function that affect the formula inside its parenthesis, that is “(b * b - (4 * a * c)).”

double root1 = ((-b) + Math.sqrt(b * b - (4 * a * c))) / 2 * a;

//Displays the result obtained for root1

System.out.println("The value of root1 is: " + root1);

 

//Quadratic formula that derives root2

double root2 = ((-b) - Math.sqrt(b * b - (4 * a * c))) / 2 * a;

 

//Displays the result obtained for root2

System.out.println("The value of root2 is: " + root2);

 

If you want a concise path to conquering variables, here’s the steps to help you achieve that:

  1. Defining variables
  2. Identifying types of variables
  3. Identifying variable naming rules and conventions
  4. Assigning and declaring variables
  5. Identifying data types
  6. Writing a quadratic application to illustrate the concepts of the blog
  7. Explaining the application code

To realize the full potential of variables, there is need to understand how to use them in mathematical expressions and the different operations that can be performed on them. The possibilities are endless! Stay tuned.

1
  • Tweet

About dennis@dennis.me.ke

What you can read next

06. Classes: Store Student Details Using Json Format
02. Learning Java in Linux
07. JAVA ARRAYS: Store Student Details Using Arrays and List

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 08. BODMAS IN JAVA

  • 07. JAVA ARRAYS: Store Student Details Using Arrays and List

  • 06. Classes: Store Student Details Using Json Format

  • 02. SPRINT RETROSPECTIVE

  • 01. SCRUM (DEMO)

Categories

  • Databases
  • Java Programming
  • OpenBaraza Framework
  • Scrum

QUICK LINKS

  • Home
  • Our Stories
  • Contact Us
  • Terms and Conditions
  • Our Policy

VISIT US

Open Baraza
Barclays Plaza, 12th Floor.
Loita Street, Nairobi.

Open in Google Maps

GET IN TOUCH

Tel: +254 (20) 2243097 / 2227100
     +254(726)209214, +254(738)819505
Email: info@openbaraza.org

Follow Us

over a year agoRT @dewcis: Manage your HR,Payroll and Time attendance with just a click of a button with our Easy to use Baraza Human Capital Management(H…
over a year agoInstalling Java Development Kit(JDK) Tutorial #Java #javascript" title="https://t.co/a2K2VzYhYq #Java #javascript" target="_blank">https://t.co/a2K2VzYhYq #Java #javascript
over a year agoNew Blog alert #java#arrays" title="https://t.co/EBDMlqL2Kq #java#arrays" target="_blank">https://t.co/EBDMlqL2Kq #java#arrays #classes
over a year agoAre you a fresh university graduate with a computer related degree and interested in being a software developer or… https://t.co/94CzteMjLq
over a year agoCheck out our new blog on operators #operators #codinglife" title="https://t.co/vOcebUw1xI #operators #codinglife" target="_blank">https://t.co/vOcebUw1xI #operators #codinglife
over a year agoLearning Java language on windows #Windows" title="https://t.co/HfsLDSWcVP #Windows" target="_blank">https://t.co/HfsLDSWcVP #Windows #Java #javascript #codinglife
over a year agoRT @dewcis: Fair Trade Software Foundation (FTSF) is venturing into a new cooperative enterprise named workpamoja. As part of the initiativ…
over a year agoWhat is SQL? Why SQL? SQLProcess? This are some the question our continuing database series article will be answeri… https://t.co/HgiUf3XwYz
over a year agoAs businesses grow, there is a need for the business owners to embrace emerging technologies. Check out our continu… https://t.co/Vq6gaF3n1o
over a year agoLike storage boxes, a database is an arranged chest of tables and columns that hold information. Check our our late… https://t.co/krRINpg03x
Follow @openbaraza
  • GET SOCIAL
Openbaraza

© All rights reserved | Open Baraza developed by DewCis Solutions.

TOP