0% found this document useful (0 votes)
40 views

CH 1java Programming

The document discusses key concepts in Java programming including: 1) The structure of a Java program with the main method and use of print statements. 2) Common loop structures like for, while, if/else. 3) Type casting between primitive data types like int to float. 4) Parsing strings to primitive types using methods like Integer.parseInt(). 5) Creating and initializing arrays, including one-dimensional, two-dimensional, and multi-dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CH 1java Programming

The document discusses key concepts in Java programming including: 1) The structure of a Java program with the main method and use of print statements. 2) Common loop structures like for, while, if/else. 3) Type casting between primitive data types like int to float. 4) Parsing strings to primitive types using methods like Integer.parseInt(). 5) Creating and initializing arrays, including one-dimensional, two-dimensional, and multi-dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

• Structure of java program:

hello.java
Import java.io.*;
Class hello //class declaration
{
public static void main(String args[]) // main method
{
System.out.println(“java is better than c”); //output or print line
}
}
• Loop structure
-Types of loops:-
1)Decision making loops- if and switch
if, if else, if else else,
2) Iterative loops – for and while

• Type Casting:- whenever a situation occurs that we need to store the


value of one type variable into another type variable is type casting.
- Syntax : datatype variable=(datatype) variable2;
- Converting from one data type into another type is casting.
- Example: int m=50;
float n=(float) m;
Type casting

SIZE in Bytes From To


1 byte Byte(8 bits) Short,char,int,long,float,
double
2 bytes Short(16 bits) int, long , float,double
2 bytes Char(16 bits) int, long , float,double
4 bytes int(32 bits) long , float,double

8 bytes Long(64 bits) Float,double


4 bytes Float(32 bits) double
• Automatic Conversion: There are some cases , where there is no need to
cast a variable but implicitly they are casted by JVM.
-Ex: byte b=75;
int a=b;
here the value of ‘b’is of byte type is stored into ‘a’ of integer type because
int is larger space than byte to hold its value: “without casting”.

• Widening and Narrowing methods for conversion casting:


• process of assigning smaller type to larger is widening or promotion.
- But process of assigning larger one to smaller type is narrowing and this
create loss of information.
Example of type casting:
Typecastdemo.java
Class Typecastdemo
{
public static void main(String args[])
{
byte b=50; short s=1996; int i=123567 ; long l=1234567894321;
Short s1=(short) b; //50
Short s2=(short) t; //error
Float nl=(float) l; //123457e+012;
System.out.println(“b=”+ b+ “ (short) b=” +s1);
}
}
• Converting string into other data types
- Integer.parselent-> Converting string into integer data type.
- Example: String str=“123”;
- int i=integer.parseint(str); //i=123;
- 1) integer.parseint
- 2)float.parsefloat
- 3)Double.parseDouble
- 4)Byte.parseByte
- 5)Short.parseShort
- 6)Boolean.parseBoolean
- 7)Long.parseLong
• Example of type conversion:
Typecastdemol.java
Class Typecastdemol
{
Public static void main(String args[])
{
String s1=‘5’, String s2=‘10’, int i,j,k;
i=integer.parseint(s1);
j=integer.parseint(s2);
K=i+j;
System.out.println(“Addition=“ +k);
}
}
• Accept values from user:
Typecastdemo2.java
Import java.io.*;
Class Typecastdemo2
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n; String s;
System.out.println(“Enter any number and String as well”);
n=Integer.parseint(br.readline());
System.out.println(“number enter= ” +n);
s=br.readLine();
System.out.println(“String Entered=” + s);
}
Array: An array is a group of related data item that share a common name.
- An array should be of a single type, comprising of integers or strings.
Array

one –dimensional two dimensional multi dimensional


array array array
1) One dimensional array: ex: char student[5]
index
Data type Array name
Step to create an array:
1. Declaring an array- syntax Type arrayname[];
or
type[] arrayname;
example: int number[]; or int [] number;
2.Creating new object of the array
syntax : arrayname=new type[size];
example: number= new int[4];
avg=new float[6];
3. Initialization of an array
syntax: arrayname[index] = value; or
type arrayname[]={list of values};
Example: number[0]=35; number[1]=20;
or int number[]={35,20,40,12,26};
int a[]={1,3,5}; int b[]; b=a;
we can obtain the size of array:-
Syntax: int variable =arrayname.length;
Example: int a=number.length;

You might also like