Skip to content

New snippets in Java #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions snippets/java/data type conversions/number-conversions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Number Conversions
description: Converts from one numeric data type to other
author: SarvariHarshitha
tags: Integer, Long, Double, Character
---

```java
public class NumericConversions {
public static void main(String[] args) {

// int to long

int i = 200;
long l = (long) i;
System.out.println(l);

// long to int

long l1 = 12334567L;
int i1 = (int) l1;
System.out.println(i1);

// int to double

int i2 = 293;
double d = (double) i2;
System.out.println(d);

// double to int

double d2 = 1374.8;
int i3 = (int) d2;
System.out.println(i3);

// char to int

char c1 = 'c';
char c2 = 'H';
int i4 = (int) c1;
int i5 = (int) c2;
System.out.println(i4);
System.out.println(i5);

// int to char
int i6 = 69;
char c3 = (char) i6;
System.out.println(c3);
}
}
```
26 changes: 26 additions & 0 deletions snippets/java/data type conversions/string-to-boolean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: String to Boolean
description: Converts String type to Boolean type
author: SarvariHarshitha
tags: String, Boolean, type-conversion
---

```java
public class StringandBoolean {
public static void main(String[] args) {

// String to Boolean
String s1 = "Hi"; // prints false
String s2 = "true";
boolean b1 = Boolean.parseBoolean(s2);
System.out.println(b1);

// Boolean to String

boolean b2 = true;
String s3 = String.valueOf(b2);
System.out.println(s3);
System.out.println(Boolean.toString(b2));
}
}
```
18 changes: 18 additions & 0 deletions snippets/java/data type conversions/string-to-char.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: String to Character
description: Converts String type to Character type
author: SarvariHarshitha
tags: String, Character, type-conversion
---

```java
public class StringandChar {
public static void main(String[] args) {

String s = "quickSnip";
char c = s.charAt(0); //Prints character at 1st index
System.out.println(c);

}
}
```
24 changes: 24 additions & 0 deletions snippets/java/data type conversions/string-to-int.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: String to Integer
description: Converts String type to Integer type
author: SarvariHarshitha
tags: String, Integer, type-conversion
---

```java
public class StringandInt {
public static void main(String[] args) {

// String to Int
String s = "200";
int i = Integer.parseInt(s);
System.out.println(i);

// Int to String
int a = 10;
String s1 = String.valueOf(a);
System.out.println(s1);

}
}
```
Loading